Basic usage of MongoDB index

Source: Internet
Author: User

The index is established by the Ensureindex method:

> db.collection.ensureIndex ({'name'1})

You can also create composite indexes:

> db.collection.ensureIndex ({'age'1'name ' 1})

Generally speaking, it is more efficient to build a composite index in the same way as Ensureindex ({sort key}, {query key}). For example, for the following actions:

> Db.collection.find ({' age ': {' $gte ': ' $lte ':}}). Sort ({' Name ': 1})

If the index is indexed in the way of age, name, the document is rarely searched (only records that are between 20-29 are retrieved), but sorting takes a lot of time. If you index by name, age, you search for a lot of documents (as per name), but you do not need to sort because the first index is established on name, so it is naturally ordered. Because the search requires only a few records to be returned, the second method is much faster than the first when the limit value is used.

You can also reverse-build the index (age:1, name:-1), which is generally the same as the sort order.

{' x ': {' $exists ': false}} can also be indexed, but inefficient because X does not exist and the X-value is null-like storage, so it has to traverse the entire document.

When you create an index, you can specify a unique value of true, which is to create a unique index:

> db.collection.ensureIndex ({'key''value'}, { ' Unique ': true})

A value of NULL or absent is considered the same case, so after a unique index is created, there can be at most one document in the collection that does not have a key key or the value of key is values. If you want a unique index to be valid only for key existence, you can establish a sparse index:

> db.collection.ensureIndex ({'key''value'}, { ' Unique ' ' Sparse ': true})

Such a unique index is valid only for the case where key exists. Note that a sparse index is not necessarily a unique index, and a unique sparse index is obtained by removing the uniqueness from the above operation. This way, when you query the value of key, a record that does not exist for key does not return.

The query process can be diagnosed using the Explain method.

Indexes are not appropriate in all cases. Indexes can be considered when the collection, the document is large, or the query is a selective query. Conversely, indexes should not be used if the collections and documents are small, or if a full table scan is required.

When you want to query all indexes in a collection, you can use the Getindexes method:

> db.collection.getIndexes ()

To delete an index using the Dropindex method, you first need to get the value of the Name field by Getindexes to specify the index that needs to be deleted.

Basic usage of MongoDB index

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.