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

Example Analysis of Index and explain in MongoDB Database

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

This article mainly introduces the MongoDB database index and explain example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Mongodb index usage

Action

Indexes can usually greatly improve queries.

An index is a data structure that collects the values of specific fields in a collection.

B-Tree index to implement.

Create an index

Db.collection.createIndex (keys, options)

Keys

Keys consists of document fields and index types. For example, {"name": 1}

Key indicates that the field value 1 represents ascending order, and-1 indicates descending order.

Options

Options for options to create an index.

Parameter type description backgroundboolean creates an index to run in the background, does not prevent other database operations uniqueboolean from creating a unique index, and the value of the document does not repeat the namestring index name. The default is: field name _ sort type starts sorting sparseboolean filters out null, fields that do not exist

View Index

Db.collection.getIndexes () {"v": 1, "key": {"_ id": 1}, "name": "_ id_", "ns": "leyue.userdatas"}, {"v": 1, "key": {"name": 1 / / index field}, "name": "name_1" / / Index name "ns": "leyue.userdatas"}

Delete index

Db.collection.dropIndex (index) deletes the specified index.

Db.collection.dropIndexes () deletes all indexes except _ id.

Index is a string that indicates that fields are deleted by index name name.

Index is {field name: 1} indicates that the index is deleted according to key.

Create / view / delete samples

View data

Db.userdatas.find () {"_ id": ObjectId ("597f357a09c84cf58880e412"), "name": "U3", "age": 32} {"_ id": ObjectId ("597f357a09c84cf58880e411"), "name": "U4", "age": 30, "score": [7,4,2,0]} {"_ id": ObjectId ("597fcc0f411f2b2fd30d0b3f"), "age": 20, "score": [7,4,2,0] 10, 9, 8,7], "name": "lihao"} {"_ id": ObjectId ("597f357a09c84cf58880e413"), "name": "U2", "age": 33, "wendang": {"yw": 80, "xw": 90}} {"_ id": ObjectId ("5983f5c88eec53fbcd56a7ca") "date": ISODate ("2017-08-04T04:19:20.693Z")} {"_ id": ObjectId ("597f357a09c84cf58880e40e"), "name": "U1", "age": 26, "address": "China Dangshan"} {"_ id": ObjectId ("597f357a09c84cf58880e40f"), "name": "U1", "age": 37, "score": [10,203,12,43,56 22]} {"_ id": ObjectId ("597f357a09c84cf58880e410"), "name": "U5", "age": 78, "address": "china beijing chaoyang"}

Create an index on the field name

/ / create index db.userdatas.createIndex ({"name": 1}) {"createdCollectionAutomatically": false, "numIndexesBefore": 1, "numIndexesAfter": 2, "ok": 1} / / View index db.userdatas.getIndexes () [{"v": 1, "key": {"_ id": 1}, "name": "_ id_" "ns": "leyue.userdatas"}, {"v": 1, "key": {"name": 1}, "name": "name_1", "ns": "leyue.userdatas"}]

Create an index for the field name and name it myindex

Db.userdatas.createIndex ({"name": 1}) db.userdatas.createIndex ({"name": 1}, {"name": "myindex"}) db.userdatas.getIndexes [{"v": 1, "key": {"_ id": 1}, "name": "_ id_", "ns": "leyue.userdatas"}, {"v": 1 "key": {"name": 1}, "name": "myindex", "ns": "leyue.userdatas"}]

The process of indexing field name is performed in the background

Creating an index when the data in the mongodb collection is too large is time-consuming and can be run in the background.

Db.userdatas.dropIndex ("myindex") db.userdatas.createIndex ({"name": 1}, {"name": "myindex", "background": true})

Create a unique index on the age field

Db.userdatas.createIndex ({"age":-1}, {"name": "ageIndex", "unique": true, "sparse": true}) db.userdatas.getIndexes () [{"v": 1, "key": {"_ id": 1}, "name": "_ id_", "ns": "leyue.userdatas"}, {"v": 1, "key": {"name": 1} "name": "myindex", "ns": "leyue.userdatas", "background": true}, {"v": 1, "unique": true, "key": {"age":-1}, "name": "ageIndex", "ns": "leyue.userdatas" "sparse": true}] / / insert an existing age db.userdatas.insert ({"name": "U8", "age": 32}) WriteResult ({"nInserted": 0, "writeError": {"code": 11000, "errmsg": "E11000 duplicate key error index: leyue.userdatas.$ageIndex dup key: {: 32.0}"}})

Create a composite index

Db.userdatas.createIndex ({"name": 1, "age":-1}) db.userdatas.getIndexes () [{"v": 1, "key": {"_ id": 1}, "name": "_ id_", "ns": "leyue.userdatas"}, {"v": 1, "key": {"name": 1, "age":-1} "name": "name_1_age_-1", "ns": "leyue.userdatas"}]

All fields are stored in the collection system.indexes

Db.system.indexes.find () {"v": 1, "key": {"_ id": 1}, "name": "_ id_", "ns": "leyue.userdatas"} {"v": 1, "key": {"_ id": 1}, "name": "_ id_", "ns": "leyue.scores"} {"v": 1 "key": {"_ id": 1}, "name": "_ id_", "ns": "leyue.test"} {"v": 1, "key": {"user": 1, "name": 1}, "name": "myindex", "ns": "leyue.test"} {"v": 1, "key": {"_ id": 1} "name": "_ id_", "ns": "leyue.mycapped"} {"v": 1, "key": {"user": 1}, "name": "user_1", "ns": "leyue.test"} {"v": 1, "key": {"name": 1}, "name": "myindex", "ns": "leyue.userdatas"}

Index summary

1: when creating an index, 1 means to store in ascending order, and-1 means to store in descending order.

2: you can create a composite index. If you want to use a composite index, you must include the first N index columns in the composite index in the query condition.

3: if the order of key values in the query condition is not the same as the order of creation in the composite index

MongoDB can intelligently help us adjust this order so that composite indexes can be used by queries.

4: you can create indexes for embedded documents, and the rules are the same as those for ordinary documents.

5: only one index can be used in a query, and $or is special. You can use one index on each branch condition.

6: $where,$exists cannot use indexes, and there are some inefficient operators, such as $ne,$not,$nin, etc.

7: when designing an index for multiple fields, you should try to put the fields used for exact matching in front of the index.

Explain usage

Grammar

Db.collection.explain ().

Explain () can set parameters:

QueryPlanner .

ExecutionStats .

AllPlansExecution .

Example

For (var iTuno Bandi)

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