Explain plan analysis helps you understand how MongoDB runs your query. It shows the steps MongoDB takes to find your data and how long it takes.
Explain plan analysis (queryPlanner, executionStats) in MongoDB
db.collection.find(query).explain('executionStats')queryPlanner shows the plan MongoDB chooses to run your query.
executionStats adds details about how many documents were scanned and how long it took.
db.users.find({ age: { $gt: 25 } }).explain('queryPlanner')db.users.find({ age: { $gt: 25 } }).explain('executionStats')queryPlanner mode if no argument is given.db.users.find({ name: 'Alice' }).explain()This inserts three users, then runs a query to find users older than 25. It shows the query plan and execution stats.
db.users.insertMany([
{ name: 'Alice', age: 30 },
{ name: 'Bob', age: 20 },
{ name: 'Carol', age: 35 }
])
const result = db.users.find({ age: { $gt: 25 } }).explain('executionStats')
printjson(result)If your query does not use an index, MongoDB will do a collection scan (COLLSCAN), which is slower.
Look at totalDocsExamined vs nReturned to see efficiency.
Use explain('executionStats') to get detailed timing and document counts.
Explain plan shows how MongoDB runs your query step-by-step.
Use queryPlanner to see the plan chosen.
Use executionStats to see how many documents were scanned and how long it took.
Practice
queryPlanner section in MongoDB's explain output show?Solution
Step 1: Understand the role of
ThequeryPlannerqueryPlannershows the strategy MongoDB picks to run the query, like which indexes it uses.Step 2: Differentiate from
executionStatsexecutionStatsshows runtime details, not the plan itself.Final Answer:
The plan MongoDB chooses to execute the query -> Option AQuick Check:
queryPlanner = plan chosen [OK]
- Confusing queryPlanner with executionStats
- Thinking queryPlanner shows query results
- Assuming queryPlanner shows errors
Solution
Step 1: Recall the correct explain verbosity
MongoDB uses'executionStats'to show runtime details.Step 2: Check option syntax
Only db.collection.find({}).explain('executionStats') uses the correct string'executionStats'withexplain().Final Answer:
db.collection.find({}).explain('executionStats') -> Option DQuick Check:
Use 'executionStats' for runtime info [OK]
- Using wrong strings like 'queryPlan' or 'runStats'
- Forgetting quotes around verbosity string
- Confusing method chaining order
{
"executionStats": {
"nReturned": 5,
"totalDocsExamined": 100,
"executionTimeMillis": 12
}
}What does
totalDocsExamined tell you?Solution
Step 1: Understand
This value shows how many documents MongoDB looked at to find matching results.totalDocsExaminedmeaningStep 2: Differentiate from
nReturnednReturnedis how many documents matched and were returned, not scanned.Final Answer:
Number of documents scanned to find matches -> Option BQuick Check:
totalDocsExamined = docs scanned [OK]
- Confusing scanned docs with returned docs
- Thinking it counts indexes used
- Assuming it is total collection size
db.users.find({age: 30}).explain('executionStat') but get an error. Which is the likely cause?Solution
Step 1: Check common explain errors
Misspelling the verbosity string causes errors because MongoDB expects exact strings.Step 2: Validate other options
Collection name and query syntax are valid; 'executionStats' is supported.Final Answer:
The verbosity string 'executionStats' is misspelled -> Option AQuick Check:
Correct spelling of verbosity string is required [OK]
- Misspelling 'executionStats' as 'executionStat'
- Assuming explain() doesn't support 'executionStats'
- Blaming query syntax without checking verbosity
totalDocsExamined: 1000 and nReturned: 2. What is the best next step?Solution
Step 1: Analyze the explain stats
HightotalDocsExaminedbut lownReturnedmeans many documents scanned unnecessarily.Step 2: Choose optimization strategy
Adding an index on query fields helps MongoDB scan fewer documents and improve speed.Final Answer:
Add an index on the query fields to reduce scanned documents -> Option CQuick Check:
Index reduces scanned docs, improves query [OK]
- Ignoring explain output and performance impact
- Increasing batch size without fixing scan issue
- Trying to return all documents unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
