MongoDB Covered Queries and Projection
On this page
Projection and covered queries
A list page shows each order's status and total. The query returns full order documents, with addresses, line items and payment details, megabytes per page that the application immediately throws away. Projection fixes the payload. A covered query can also avoid reading the documents at all.
Projection syntax
The second argument to find picks fields:
db.orders.find({ customerId: 4021 }, { status: 1, total: 1 }) // include: status, total (+ _id)
db.orders.find({ customerId: 4021 }, { lines: 0, payment: 0 }) // exclude these
db.orders.find({ customerId: 4021 }, { status: 1, "shipTo.city": 1 }) // nested field
db.orders.find({ customerId: 4021 }, { lines: { $slice: 3 } }) // first 3 array elementsYou can't mix inclusion and exclusion in one projection, except to exclude _id. Projection reduces network transfer and deserialization in your application. On its own, the server still reads each full document from storage.
Covered queries: answering from the index alone
If every field the query filters on and returns is in one index, MongoDB can answer from the index alone. Tested on MongoDB 9 with an index on { customerId: 1, status: 1, total: 1 }:
db.orders.find({ customerId: 4021, status: "paid" }, { status: 1, total: 1 })
// winning plan: PROJECTION_SIMPLE keys examined 1 docs examined 1
db.orders.find({ customerId: 4021, status: "paid" }, { _id: 0, status: 1, total: 1 })
// winning plan: PROJECTION_COVERED keys examined 1 docs examined 0The only difference is _id: 0. _id is returned by default and isn't in the index, so the first query has to fetch the document to get it. Excluding it makes the query covered: zero documents read.
Rules for a covered query
- Every filter field is in the index.
- Every returned field is in the index; exclude
_idunless it's indexed as part of the compound index. - No field in the filter or projection is an array in any document (multikey indexes can't cover queries on the array field).
- Fields inside embedded documents can be covered if the index is on the dotted path.
When covered queries are worth it
Covering matters most for high-volume, read-heavy queries on large documents, where reading documents from disk or cache dominates. Examples: listing ids and statuses, existence checks, counts with countDocuments on indexed conditions, and lookups of a few fields from big documents.
Don't stuff every field into an index to cover everything: wider indexes use more memory and slow writes (Day 7). Cover the few hottest queries.
Check yourself
_id is returned by default and isn't part of the index, so MongoDB must fetch each document to get it. Exclude it with _id: 0 in the projection, and the query can be answered from the index alone.
Projection only limits which fields are sent back, which saves network but still reads each document. A covered query also avoids reading documents at all, because the index contains every field used to filter and return, so explain shows no FETCH stage.
The index stores separate entries per array element, so it can't reconstruct the field's full array value without reading the document.
In explain output, totalDocsExamined is 0 and there's no FETCH stage, only an IXSCAN followed by a projection. That proves MongoDB answered entirely from the index without reading any documents.
For very frequent queries that return a few small fields, such as lookups by email that return only an id and name. For rare queries, a bigger index just to cover them usually isn't worth the write and memory cost.