MongoDB Covered Query & Analyzing Query with Example
Let's dive into MongoDB Covered Query and Analyzing Query with clear examples.
MongoDB Covered Query
A covered query in MongoDB is a query that uses an index and doesn’t need to scan the actual documents. An index covers a query if the following conditions are met:
All fields used in the query are part of an index.
All fields returned in the result are from the same index.
Example:
Suppose you have a collection named example with indexes on the fields type and item:
db.example.createIndex({ type: 1, item: 1 })
This index will cover queries that operate on the type and item fields and return only the item field:
db.example.find( { type: "game", item: /^c/ }, { item: 1, _id: 0 } )
Note: In earlier versions, you had to explicitly exclude the _id field to cover the query. Since MongoDB 3.6, an index can also cover queries on fields inside embedded documents.
Example 1
Consider a collection called tutorials with documents like:
{ _id: 1, user: { login: "student" } }
And an index:
{ "user.login": 1 }
A covered query example would be:
db.tutorial.find( { "user.login": "student" }, { "user.login": 1, _id: 0 } )
Example 2
Let's take another case:
Document in the examples collection:
{ "_id": ObjectId("53402597d852426020000002"), "contact": "1234567809", "dob": "01-01-1991", "gender": "M", "name": "ABC", "user_name": "abcuser" }
Create a compound index:
db.examples.ensureIndex({ gender: 1, user_name: 1 })
Now, this query will be covered:
db.examples.find({ gender: "M" }, { user_name: 1, _id: 0 })
From these examples, we understand that MongoDB can fetch data directly from the index without accessing the actual documents, making queries faster and more efficient.
Important: An index cannot cover a query if:
Any indexed field is an array.
Any indexed field is a subdocument.
MongoDB Analyzing Query
Analyzing queries is crucial for evaluating database performance and query design. In MongoDB, we mainly use two operations: $explain and $hint.
1. $explain
The $explain operator provides detailed information about how MongoDB executed a query — such as which indexes were used, the number of documents scanned, and overall performance stats.
Example:
Using the same examples collection:
db.examples.ensureIndex({ gender: 1, user_name: 1 })
Running an $explain:
db.examples.find({ gender: "M" }, { user_name: 1, _id: 0 }).explain()
The output would look something like this:
{ "cursor": "BtreeCursor gender_1_user_name_1", "isMultiKey": false, "n": 1, "nscannedObjects": 0, "nscanned": 1, "nscannedObjectsAllPlans": 0, "nscannedAllPlans": 1, "scanAndOrder": false, "indexOnly": true, "nYields": 0, "nChunkSkips": 0, "millis": 0, "indexBounds": { "gender": [ [ "M", "M" ] ], "user_name": [ [ { "$minElement": 1 }, { "$maxElement": 1 } ] ] } }
Understanding the output:
indexOnly: true indicates that the index was sufficient to fulfill the query.
cursor: "BtreeCursor" shows the type of cursor used (index-based).
n shows the number of documents matched.
nscannedObjects is the total number of documents scanned.
nscanned shows the number of index entries scanned.
2. $hint
The $hint operator forces MongoDB to use a specific index for query execution. This is helpful when you want to manually test or optimize queries with different indexes.
Example using $hint:
db.examples.find({ gender: "M" }, { user_name: 1, _id: 0 }).hint({ gender: 1, user_name: 1 })
Summary
In this Debugshala tutorial, we learned about MongoDB Covered Queries and Analyzing Queries with examples. We explored how $explain helps understand query execution and how $hint can enforce index usage for performance testing.
Write A Comment
No Comments