Get the App
SLTechnology News&Howtos  ›  Database  › 

What the index classification is and how to create the index in mongodb

Shulou Source: shulou.com Published: 2022-05-31 17:37:44 09月20日 Update

It is believed that many inexperienced people have no idea about how to classify indexes in mongodb and how to create indexes. Therefore, this paper summarizes the causes and solutions of the problems. Through this article, I hope you can solve this problem.

Learn index classification and create indexes:

1._id index default index for most collections

two。 Single-key index: manually created, a single value

3. Multi-indexing: composite function

4. Compound index: leftmost prefix principle

5. Expired index: expired within a certain period of time, note: it must be isodate or its array, do not use timestamps, otherwise it will not be automatically deleted.

6. Full-text index db.tm.ensureindex ({"article": "text"}), db.tm.ensureindex ({"key1": "text", "key2": "text"}), db.tm.ensureindex ({$* *: "text"})

Query: db.tm.find ({$text: {$search: "aa"}})

Db.tm.find ({$text: {$search: "aa bb cc"}})

Db.tm.find ({$text: {$search: "aa bb-cc"}})

Db.tm.find ({$text: {$search: "\" a\ "\" bb "\" cc\ "}})

Matching degree of full-text index $meta

Db.tm.find ({$text: {$search: "aa bb"}}, {score: {$meta: "textScore"}})

Db.tm.find ({$text: {$search: "aa bb"}}, {score: {$meta: "textScore"}}) .sort ({score: {$meta: "textScore"}})

Only $text can be specified at a time

7. Geographical location Index (Didi Taxi, Dianping)

2d index (address location index)

Db.localtion.ensureindex ({"w": "2d"})

View the index: db.tm.getIndexes ()

Index db.t1.ensureIndex ({XRV 1})

Multi-key index db.tm.ensureIndex ({x: [1meme 2meme 3meme 4pr 5]})

Composite index db.tm.ensureIndex ({XRX 1BI YRO 1})

Delete index db.tm.dropIndex ("x_1_y_1")

Db.tm.find ({XRV 100). Explain ()

Expired index: db.tm.insert ({time:new Date ()}) ISOdate is the current time

Db.tm.ensureIndex ({time:1}, {expireAfterSeconds:10})

Db.tm.insert ({time:new Date (), zv1})

Full-text index

Db.t1.ensureIndex ({article: "text"})

Db.t1.insert ({article: "aa bb cc"})

Find db.t1.find ({$text: {$search: "aa"}})

Db.t1.find ({$text: {$search: "aa bb cc"}}) or relationship

Db.t1.find ({$text: {$search: "aa bb-cc"}}) does not contain CC

The relationship between db.t1.find ({$text: {$search: "\" aa\ "\" bb\ "- cc\"}}) and

Similarity of full-text index: db.t1.find ({$text: {$search: "aa bb"}}, {score: {$meta: "textScore"}})

Db.t1.find ({$text: {$search: "aa bb"}}, {score: {$meta: "textScore"}}) .sort ({score: {$meta: "textScore"}})

Index naming

Db.t1.ensureIndex ({name: "xyz"})

Db.t1.dropIndex ("xyz")

Create a unique index

Db.t2.ensureIndex ({mvlv 1, {unique:true})

Check that there is a field in the s index

Db.abc.find ({m: {$exists:true}})

Create a 2d index: flat geolocation index, location representation, latitude and longitude [longitude (- 180180), latitude (- 90BE90)]

Db.location.ensureIndex ({"w": "2d"})

Db.location.insert ({w: [1J 1]})

Db.location.insert ({w: [1J 2]})

Db.location.insert ({w: [3jue 2]})

Db.location.insert ({w: [32, 522]})

Db.location.insert ({w: [1000.90]})

Nearest inquiry

Db.location.find ({w: {$near: [1Jing 1]}})

Query

Db.location.find ({w: {$near: [1Jing 1], $maxDistance:10}})

Address location index

GeoNear

Db.runCommand ({geoNear: "location", near: [1J2], maxDistance:10,num:1})

Db.stats

For (iTunes 1)

Tags: Index location full text query classification time content address geography location method more level problem similarity helpless for this function prefix single key Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi MariaDB MySQL Linux Docker