Explanations of Covered Queries in MongoDB

If You are interested to learn about the Database References

What is a Covered Query?

A covered query is one that doesn’t need to look through any documents and can be fully satisfied by an index. When both of the following conditions are met, a query is covered by an index: all of the fields in the query are included in an index; and all of the fields returned in the results are included in the same index.

A covered query, according to the official MongoDB manual, is one that:

The query’s fields are all included in an index.

The query’s returned fields are all contained in the same index.

MongoDB matches the query conditions and returns the result using the same index without actually searching within the documents because all the fields provided in the query are a part of an index. Since indexes are kept in RAM, retrieving data from them is much quicker than retrieving data by document scanning.

Using Covered Queries

To test covered queries, consider the following document in the users collection −

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

We will first create a compound index for the users collection on the fields gender and user_name using the following query −

>db.users.createIndex({gender:1,user_name:1})
{
	"createdCollectionAutomatically" : false,
	"numIndexesBefore" : 1,
	"numIndexesAfter" : 2,
	"ok" : 1
}

Now, this index will cover the following query −

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

That is to say, MongoDB would not search database documents for the aforementioned query. Instead, it would quickly retrieve the needed information from indexed data.

Since MongoDB by default returns the _id column in every query, we have specifically excluded it from the result set of our query because our index does not contain the _id field. Consequently, the index built above would not have included the following query.

>db.users.find({gender:"M"},{user_name:1})
{ "_id" : ObjectId("534025
97d852426020000003"), "user_name" : "tombenzamin" }

Finally, keep in mind that a query cannot be covered by an index if

  • Each index field contains an array.
  • A subdocument is any field that is indexed

Why is a covered query important?

We can query data more quickly by using covered queries. This is accomplished by making sure the index that is built has each and every field needed by the query. Other than the ones that are indexed, no other documents need to be examined. We must make sure that the index includes every field in the query as well as the results returned.

What is advantage of using covered query in MongoDB?

Without having to scan such big data, all the data is instantly fetched in this scenario (from the same index that was already loaded into RAM) (documents)

Creating Covered Index (create compound index on field firstName and salary) >

> db.employee.ensureIndex({firstName  : 1}, {salary : 1} )

Now, covered index have been created on field firstName and salary.

Now, let’s query/read data from covered indexes >

Now, covered index will be available when we use following query >

db.employee.find({firstName:”Ankit”},{salary:1, _id :0})

Output>

{ “salary” : 1000 }

In above query,

The covered index (on firstName and salary) is loaded into RAM when we locate a document with the firstName Ankit, and since we are aware that the salary is also contained in the compound index, it is read rapidly. This prevents us from scanning an excessive number of documents in the database.

Now, covered index will NOT be available when we use following query >

db.employee.find({firstName:”Ankit”},{salary:1})

Output>

{ “_id” : ObjectId(“588103a41e509423fa0faa6b”), “salary” : 1000 }

In above query,

We CANNOT prevent scanning too many documents in the database when we locate a document with firstName=”Ankit,” as covered index (on firstName and salary) is loaded into RAM, but by default _id (i.e. the column not present in covered index) is also displayed.

Now, covered index will also NOT be available when we use following query >

db.employee.find({firstName:”Ankit”})

Output>

{ “_id” : ObjectId(“588103a41e509423fa0faa6b”), “firstName” : “Ankit”, “lastName” : “Mittal”, “salary” : 1000 }

Here we can see that fields (_id and lastName) not present in covered index are displayed.

Where covered index does not work?

covered index does not work when field on which we are trying to cover in covered index >

  • Array or
  • Embedded document (or a sub document)
Explanations of Covered Queries in MongoDB
Show Buttons
Hide Buttons