Covered Query in MongoDB

What is Covered Query?

In the official MongoDB Documentation, a Covered Query is a query in which:

All fields in the query are part of an index and

All fields returned in the query are in the same index.

When all fields in the query are part of the index, MongoDB connects query conditions and returns the result by using the same index without looking inside the Document. When indexes are present in RAM, retrieving data from indexes is faster when compared to retrieving data by scanning all documents.

Use Covered Query in MongoDB

To study Covered Query, you follow the following Document in the user collection:

 { "_id" : ObjectId ( "53402597d852426020000002" ), "contact" : "987654321" , "dob" : "01-01-1991" , "gender" : "M" , "name" : "Tom Benzamin" , "user_name" : "tombenzamin" } 

First, we create a complex index for users collection on the gender and user_name fields using the following query:

 > db . users . ensureIndex ({ gender : 1 , user_name : 1 }) 

Now, this index will cover the following query:

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

It can be said that, with the above query, MongoDB will not go into searching for documents in the database. Instead, it will retrieve the necessary data from indexed data, which will make the process much faster.

When our index does not include the _id field, we explicitly exclude it from the result set of the query, because with MongoDB, by default it will return the _id field in each query. Therefore, the following query will not be covered within the index created above:

 > db . users . find ({ gender : "M" },{ user_name : 1 }) 

Finally, remember that an index cannot cover a query if:

Any indexed field is an array

Any indexed field is a Subdocument

According to Tutorialspoint

Previous article: Reference Database in MongoDB

Next lesson: Query analysis in MongoDB

3.8 ★ | 5 Vote

May be interested

  • MongoDB malicious code attacks more than 26,000 victims in a weekMongoDB malicious code attacks more than 26,000 victims in a week
    malware that attacks the mongodb database has rekindled last week and after the weekend with the arrival of three new groups hijack more than 26,000 servers, of which one group attacked 22,000 machines.
  • Learn about security features and authentication in MongoDBLearn about security features and authentication in MongoDB
    in the following article, we will continue to introduce security and authentication features in the mongodb database management system series. in essence, the basic account validation feature is available in mongodb, but has been disabled in default mode, and in this mode the system requires at least 1 active database ...
  • ObjectId in MongoDBObjectId in MongoDB
    you have seen the use of mongodb objectid in previous chapters. in this chapter, we will understand the structure of objectid.
  • Text Search in MongoDBText Search in MongoDB
    starting with version 2.4, mongodb started supporting text indexes to search within the string content.
  • Action Query in Action 2016Action Query in Action 2016
    action queries are queries that perform actions on data, can add, change or delete records.
  • How to have Siri respond to your voice when the iPhone screen is coveredHow to have Siri respond to your voice when the iPhone screen is covered
    in ios 13.4, apple added an option to always listen to hey siri‌ regardless of the location of your phone. just follow the steps below to change this setting and siri‌ will always be ready to respond to your query or command.
  • Shard in MongoDBShard in MongoDB
    sharding is a process of storing data records across multiple devices and it is a method of mongodb to meet the requirement for increasing data. when the size of the data increases, a single device cannot be enough to store data.
  • Learn about Java Driver in MongoDBLearn about Java Driver in MongoDB
    in the following article, we will introduce you some basic features of mongodv java driver as well as how to deploy and apply in practice.
  • Install MongoDBInstall MongoDB
    instructions for installing mongodb on windows.
  • Multiple choice questions have a Query optionMultiple choice questions have a Query option
    query is a language used in databases and information systems. the following is an invitation to test your knowledge with network administrator through multiple-choice questions below.