What is a covered query?
A covered query is one that can be answered entirely from an index — MongoDB never reads the actual document. Drastically faster.
Conditions — all fields in the query and projection must be in the index, and the projection must exclude
_id(or include it explicitly via the index).Verify with explain() — look for
IXSCANwith noFETCHstage.Especially useful — for high-frequency lookups, count queries, and pagination keys.
Trade-off — bigger compound indexes slow writes and use more RAM.
// Index on { name: 1, email: 1, age: 1 }
db.users.createIndex({ name: 1, email: 1, age: 1 });
// COVERED query — all fields from index
db.users.find(
{ name: 'John' }, // Filter: name is in index
{ name: 1, email: 1, _id: 0 } // Projection: name, email in index, _id excluded
);
// explain() shows: totalDocsExamined: 0 (no documents accessed!)
// NOT covered — _id included (default)
db.users.find(
{ name: 'John' },
{ name: 1, email: 1 } // _id is included by default — not in index!
);
// Must access documents to get _id
// NOT covered — field not in index
db.users.find(
{ name: 'John' },
{ name: 1, phone: 1, _id: 0 } // phone not in index
);The first query is covered: filter (name) and projection (name, email) are all in the index, and _id is excluded. MongoDB reads only the index — zero documents accessed.
The second query is NOT covered because _id is included by default. The third is NOT covered because phone is not in the index.
Covered query = all filter + projection fields in the index + _id excluded. totalDocsExamined: 0 in explain proves it is covered. The _id exclusion requirement is the most commonly missed detail.
Covered queries are the fastest possible queries.