Explorer
MongoDB

Explain Covered Queries

Covered Queries in MongoDB

A Covered Query represents the pinnacle of query optimization in MongoDB. It occurs when all fields requested by the query and projection are stored inside an index, allowing MongoDB to return results directly from RAM without reading the underlying documents from disk.

Conditions for a Covered Query

Condition Requirement Why It Matters
1. Query Filter All fields in the query filter must exist in the index Ensures index scan covers matching logic
2. Projected Fields All fields returned in projection must exist in the index No un-indexed field needs to be retrieved
3. Explicit _id Suppression _id: 0 must be set (unless _id is part of index) Because _id is returned by default, omitting suppression forces a document lookup

Covered vs. Non-Covered Execution Stats

Execution Metric Non-Covered Query Covered Query
Winning Plan FETCH > IXSCAN IXSCAN (No FETCH stage)
totalDocsExamined > 0 (documents fetched from disk) 0 (zero disk lookups)
Latency Bounded by disk/page-cache lookups Sub-millisecond RAM lookups

Example

CODE
// 1. Create compound index
db.users.createIndex({ dept: 1, name: 1 });

// 2. Covered query (totalDocsExamined = 0)
db.users.find({ dept: "Engineering" }, { name: 1, dept: 1, _id: 0 });

Finished this lesson?

Mark this chapter complete to update your learning streak and unlock the next lesson.