The index of MongoDB

Source: Internet
Author: User
Tags mongodb query

  First, the concept of the index

I believe that everyone is not unfamiliar with the index, the index of the database is similar to the directory index of the book, there is an index, reading when you do not have to go through the whole book, you can directly jump to the target content according to the number of pages, improve reading and query efficiency. Database index is the same, it is used to improve the query speed, indexed, MongoDB query can be found in the index after the entry, directly to the target collection location.

  Second, the default index

  When retrieving the system index, it can be found that MongoDB defaults to a default "_id" index for each collection, which is used as the index to reference when retrieving. That is, our usual find operation is by default based on the "_id" index to find.

> Db.system.indexes.find (); {"V": 1, "key": {"_id": 1}, "ns": "Kaiye.c1", "name": "_id_"} {"V": 1, "key": {"_id": 1}, "ns": "Kaiye.c2", "name": "_id_"} {"V": 1, "key": {"_id": 1}, "ns": "kaiye.c3", "name": "_id_"}

Third, why to build a custom index

    First add 1 million data to a set of C4, then retrieve the age=100 data, and call the explain function to calculate the information in the retrieval process, found that in the absence of sorting, no custom index, without querying a data, need to scan 1 million data, And just a simple query takes 546 milliseconds, which is unacceptable in a large data-volume business system.

for (Var i=0;i<1000000;i++) {
Db.c4.insert ({name: "Diao", age:i})

}

    

  Iv. Comparison of Custom indexes

    First, the name field of the C4 collection is indexed, the keyword is ensureindex, the syntax is Db.c4.ensureIndex ({age:1}), and then the query age=100 operation is performed, retrieving information such as

At this point, you can find that after indexing the age field, a simple query is made, and the number of bars traversed is only one, and it takes almost 0, and the effect is generally visible compared to index-less retrieval.

    

Iv. Other operations on the index

(1) Information about the query index:db. Collection. Stats ();

(2) Delete index: db. Collection. dropindex ({indexed field: 1})

       Deleting a collection also deletes all the indexes in the collection

     (3) Create a unique index: A unique index means that when a field is set to a unique index, the field in the collection, the same value is not allowed to exist, that is, the value is unique, such as the above case, and then insert a document AGE=100, there will be an error message.

Syntax: DB. Collection. Ensureindex ({age:1},{unique:true})

The index of MongoDB

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.