Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

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)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report