MongoDB Overlay Index Query

Source: Internet
Author: User
Tags mongodb documentation

The official MongoDB documentation shows that the overwrite query is the following query:

    • All query fields are part of the index
    • All the query return fields are in the same index

Because all fields that appear in the query are part of the index, MongoDB does not need to retrieve matching query criteria throughout the data document and return query results that use the same index.

Because the index exists in RAM, getting data from the index is much faster than reading the data by scanning the document.

Using the Overwrite index query

In order to test the cover index query, use the following users collection:

{   "_ID": ObjectId "53402597d852426020000002"  :  "987654321"   "DOB" :  "01-01-1991" Span class= "pun" >,  "gender" :  "M" ,  "name" : ,  "user_name" :   "tombenzamin" }      

We create federated indexes in the Users collection, fields Gender and user_name:

>db.  Users.  Ensureindex({gender:1,user_name:1})     

The index now overwrites the following query:

>db.  Users.  Find({gender:"M"},{user_name:1,_id:0}) 

In other words, for the above query, MongoDB will not go to the database file to find. Instead, it extracts the data from the index, which is a very fast data query.

Since the _id field is not included in our index, _ID is returned by default in the query, and we can exclude it from the query result set in MongoDB.

The following instance does not exclude _id and the query will not be overwritten:

>db.  Users.  Find({gender:"M"},{user_name:1})      

Finally, if the query is the following, you cannot use the Overwrite index query:

    • All indexed fields are an array

MongoDB Overlay Index Query

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.