0
0
MongoDBquery~10 mins

Covered queries with indexes in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Covered queries with indexes
Query issued
Check index for fields
Index covers all query fields?
NoFetch full document from collection
Yes
Return results directly from index
Query completes faster
The database checks if the index contains all fields needed by the query. If yes, it returns data from the index only, skipping the full document fetch.
Execution Sample
MongoDB
db.users.createIndex({name:1, age:1, city:1})
db.users.find({name:'Alice'}, {age:1, city:1, _id:0})
Create an index on name, age, city fields and run a query that requests only those fields, enabling a covered query.
Execution Table
StepActionIndex Fields CheckedQuery FieldsCovered?Data SourceResult
1Query issuedname, age, cityname, age, cityYesIndex only{age: 30, city: 'NY'}
2Return resultname, age, cityage, cityYesIndex onlyResult sent to client
3Query ends----No need to fetch full document
💡 Query fields are fully covered by the index, so no document fetch is needed.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
Index Fieldsnonename, age, cityname, age, cityname, age, city
Query Fieldsnonename, age, cityage, cityage, city
Data SourcenoneIndex onlyIndex onlyIndex only
Key Moments - 2 Insights
Why does the query not fetch the full document?
Because the index contains all the fields requested by the query, the database can return results directly from the index without fetching the full document (see execution_table step 1).
What happens if the query requests a field not in the index?
The database must fetch the full document from the collection to get the missing field, so the query is not covered (not shown here but implied by the 'No' branch in concept_flow).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the database decide the query is covered by the index?
AStep 1
BStep 2
CStep 3
DBefore Step 1
💡 Hint
Check the 'Covered?' column in execution_table row for Step 1.
According to variable_tracker, what is the data source after Step 2?
AFull document fetch
BIndex only
CNo data source
DPartial index and partial document
💡 Hint
Look at the 'Data Source' row under 'After Step 2' in variable_tracker.
If the query requested a field not in the index, what would change in the execution_table?
AThe query would still be covered
BThe index fields would change
CThe 'Covered?' column would say 'No' and data source would be 'Full document fetch'
DThe query would return no results
💡 Hint
Refer to the concept_flow where the 'No' branch leads to fetching the full document.
Concept Snapshot
Covered queries use indexes that include all requested fields.
If all query fields are in the index, MongoDB returns data from the index only.
This avoids fetching full documents, making queries faster.
Create indexes including all fields you want to query and return.
Covered queries improve performance by reducing disk reads.
Full Transcript
Covered queries with indexes happen when the database can answer a query using only the index without fetching the full document. This requires the index to contain all fields the query filters on and returns. When a query is issued, MongoDB checks if the index covers all needed fields. If yes, it returns results directly from the index, speeding up the query. If not, it fetches the full document. Creating indexes that cover your query fields enables covered queries and improves performance.