executionStats is an explain() verbosity mode that combines the selected query plan with statistics from query execution. Use it to diagnose inefficient queries and verify whether an index is helping. section 7.1.1
The output helps connect the plan to observed work:
- queryPlanner describes the winning plan; executionStats adds returned-document counts, execution time, keys examined, and documents examined. section 7.1.1
- A winningPlan commonly forms a stage tree: IXSCAN reads matching index keys, FETCH retrieves documents, and the top stage produces results. section 7.1.1
- Compare examined keys and documents with nReturned to assess how selectively the query is being served. section 7.1.1
- Use executionStats after creating an index to validate actual use, including indexed filtering and sorting. section 7.2.1
Inspect a movie query
Run db.movies.find({ "runtime": 100 }).explain("executionStats") after creating runtime_1. The output can show an IXSCAN followed by FETCH, nReturned: 1, executionTimeMillis: 3, totalKeysExamined: 1, and totalDocsExamined: 1. section 7.2.1
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
)
Compare the default queryPlanner mode, executionStats, and allPlansExecution directly on the same query. section 7.1.1
Listing 7.2 Execution-plan output structure
{
"winningPlan": {
"stage": "FETCH",
"inputStage": {
"stage": "IXSCAN",
"indexName": "yourField_1",
"keyPattern": {
"yourField": 1
},
"indexBounds": {
"yourField": [
"[\"value\", \"value\"]"
]
}
}
},
"executionStats": {
"nReturned": 1,
"executionTimeMillis": 3,
"totalKeysExamined": 1,
"totalDocsExamined": 1
}
}
Inspect the compact relationship between winningPlan stages and executionStats values. section 7.1.1
