totalDocsExamined reports how many documents MongoDB examines while executing an operation. Compare it with nReturned and totalKeysExamined to judge whether an index limits unnecessary document scanning. section 7.1.1
Interpret the metric alongside the winning execution stage:
- An IXSCAN followed by FETCH can examine only the documents matching an indexed lookup; one returned document and totalDocsExamined: 1 indicate an efficient query. section 7.1.1
- A COLLSCAN examines the collection when no suitable index supports the query, potentially producing a large totalDocsExamined value. section 7.2.2
- A high ratio of examined documents to returned documents signals an opportunity to review query predicates and index design, including compound-index prefixes. section 7.2.2
Diagnosing an unsupported query
Running db.movies.find({ "imdb.rating": { $gte: 7.6 } }).explain("executionStats") produces a COLLSCAN with totalDocsExamined: 21349, totalKeysExamined: 0, and nReturned: 3472. The query examines every document because the available compound index does not begin with imdb.rating. section 7.2.2
Listing 7.1 Appending the MongoDB explain () method
db.collection.find(
{ yourField: "value" }
).explain() // Explain the default query planner information
db.collection.find(
{ yourField: "value" }
).explain(
"executionStats" // Explain the query execution statistics
)
db.collection.find(
{ yourField: "value" }
).explain(
"allPlansExecution" // Explain the query execution
➥plan with all possible query plans
)
Run explain("executionStats") to expose totalDocsExamined together with returned-document counts, execution time, and examined index keys. section 7.1.1
