MongoDB deep-dive · Part 2 of 11. Previous: Indexes in depth. Next: Write concerns & read concerns.
If Part 1 is about placing the right bet, this part is
about reading the receipts. explain() is MongoDB’s way of showing you what
plan it chose, what every stage of that plan cost, and where the work actually
happened. The numbers are dense if you approach them without a framework. With
one, they’re immediately legible.
The three verbosity modes
# Just the winning plan — no execution stats
col.find(query).explain()
# Winning plan + how much work was done
col.find(query).explain("executionStats")
# All candidate plans + their stats (expensive — not for production)
col.find(query).explain("allPlansExecution")
"executionStats" is what you want 95% of the time. It tells you what the
planner chose and how much it cost to execute. "allPlansExecution" is useful
when you suspect the planner is picking the wrong index — it shows you the
plans that lost and why.
The key numbers
When you call .explain("executionStats"), the top-level executionStats
object contains the numbers that matter:
| Field | What it means |
|---|---|
nReturned | Documents returned to the application |
totalKeysExamined | Index entries scanned |
totalDocsExamined | Full documents loaded from the collection |
executionTimeMillis | Total wall-clock time for the query |
The ratio to watch: totalDocsExamined / nReturned. A ratio of 1 is ideal —
you loaded exactly the documents you returned. A ratio of 1000 means you
inspected 1000 documents to return 1. This is the signal for a missing or
mismatched index, or a predicate that can’t use an index at all.
Reading the winning plan tree
The winningPlan is a tree of stages. Each stage has a stage field (what
it did) and optionally an inputStage (what fed it). Read from the innermost
stage outward — that’s the execution order.
{
"winningPlan": {
"stage": "FETCH",
"inputStage": {
"stage": "IXSCAN",
"keyPattern": {"user_id": 1, "status": 1},
"indexBounds": {
"user_id": ["[\"u-42\", \"u-42\"]"],
"status": ["[\"active\", \"active\"]"]
}
}
}
}
This plan reads: IXSCAN (walk the index for matching keys) → FETCH (load
the full documents for those keys). The innermost stage always executes first.
Stage vocabulary
| Stage | Meaning |
|---|---|
COLLSCAN | Full collection scan — no usable index |
IXSCAN | Index scan — walking the B-tree |
FETCH | Load full documents from the collection |
SORT | In-memory (or on-disk) sort — index couldn’t satisfy .sort() |
SORT_MERGE | Merge multiple sorted index scans |
PROJECTION_COVERED | Projection served from index (no FETCH) |
LIMIT | Apply a .limit() |
SKIP | Apply a .skip() |
What FETCH means and when it’s unavoidable
FETCH appears when the index can narrow down the candidates but the query
projection needs a field that isn’t in the index. MongoDB has no choice but to
load the full document to retrieve that field.
FETCH is not inherently bad. If you’re returning a large projection of
non-indexed fields, FETCH is the right behaviour. It’s only a problem when
the index scan produces many candidates that then require a FETCH, but most
of those candidates are filtered out after the fetch by predicates on
non-indexed fields. That pattern — IXSCAN producing 10,000 candidates, FETCH
loading 10,000 documents, only 50 surviving a final filter — is what explodes
totalDocsExamined.
The fix is usually one of:
- Extend the compound index to include the filtered field (push the predicate into the index scan).
- If the projection only needs indexed fields, exclude
_idto achieve a covered query.
What SORT means and when to worry
SORT appears in the winning plan when the query has a .sort() clause that
the index cannot satisfy. MongoDB has to load all matching documents, hold them
in a sort buffer in RAM, sort them, then return them.
The in-memory sort buffer default is 100 MB. If the sort working set exceeds that, MongoDB either rejects the operation with an error or spills to disk (depending on configuration). Either way, it’s slow.
A worked example: diagnosing a slow query
Say you’re seeing 300ms queries on this:
db.orders.find(
{"customer_id": "c-100", "ts": {"$gte": t_start, "$lte": t_end}},
{"amount": 1, "status": 1}
).sort("amount", -1)
You run:
plan = db.orders.find(
{"customer_id": "c-100", "ts": {"$gte": t_start, "$lte": t_end}},
{"amount": 1, "status": 1}
).sort("amount", -1).explain("executionStats")
stats = plan["executionStats"]
print(f"nReturned: {stats['nReturned']}")
print(f"totalDocsExamined: {stats['totalDocsExamined']}")
print(f"totalKeysExamined: {stats['totalKeysExamined']}")
print(f"executionTimeMillis:{stats['executionTimeMillis']}")
Output:
nReturned: 48
totalDocsExamined: 48
totalKeysExamined: 48
executionTimeMillis:287
The ratio is 1:1 — that’s healthy. So the scan isn’t the problem. You look at the winning plan:
{
"winningPlan": {
"stage": "SORT",
"sortPattern": {"amount": -1},
"memUsage": 2048000,
"inputStage": {
"stage": "FETCH",
"inputStage": {
"stage": "IXSCAN",
"keyPattern": {"customer_id": 1, "ts": 1}
}
}
}
}
There it is: SORT at the top. The index {customer_id, ts} exists and is
being used for the equality and range predicates, but amount isn’t in it, so
the sort has nowhere to go. The fix is an ESR-ordered compound index:
db.orders.create_index([
("customer_id", ASCENDING), # E — equality
("amount", DESCENDING), # S — sort field, matches sort direction
("ts", ASCENDING), # R — range
])
After creating this index, the winning plan becomes IXSCAN → FETCH with no
SORT stage. The sort is free — it falls out of the index order. Latency
drops.
The SBE engine (MongoDB 8.0+)
Since MongoDB 8.0, many queries are executed by the Slot-Based Execution
(SBE) engine rather than the classic interpreter. When SBE is active, the
explain() output gains a slotBasedPlan field under queryPlanner. The
high-level stage vocabulary (IXSCAN, FETCH, SORT) is preserved, so your
diagnostic workflow doesn’t change. The difference is that SBE executes queries
as compiled slot-passing programs rather than interpreted tree-walks, which
reduces per-document overhead for filter-heavy queries.
If you see slotBasedPlan in the output and your query falls into an SBE
category, the execution times you’re measuring already include the SBE speedup.
You don’t need to do anything differently — just be aware that benchmarks from
MongoDB 7.x and earlier will show improvements on the same hardware once you
upgrade.
Quick diagnostic checklist
When a query is slow:
- Run
explain("executionStats"). - Check for
COLLSCANin the winning plan → missing index. - Check
totalDocsExamined / nReturnedratio → if >> 1, index is scanning too many candidates. - Check for
SORTstage → ESR violation or missing sort field in index. - Check
totalDocsExamined→ if 0, query is covered (rarely the problem). - If plan seems wrong, run
explain("allPlansExecution")to see rejected plans.
The two most common findings: a COLLSCAN on an unindexed field, and a SORT
stage caused by an ESR violation. Together they cover the majority of
query performance incidents.
explain()doesn’t tell you what index to create — it tells you what work was done without the right index. The question “why is this SORT stage here?” has one answer 90% of the time: the sort field is not in the compound index, or it’s in the wrong position per ESR.
Recall · check yourself
Before you go
Answer from memory first, then reveal. These cards also live in the review queue — come back in a few days.
-
R01
totalDocsExamined: 0with non-zeronReturnedmeans what?A covered query — answered entirely from the index, no documents loaded. The gold standard for read-heavy paths.
-
R02 A SORT stage appears even though the sort field is in the compound index. Why?
Key order misalignment — the sort field sits before or after the range field instead of between equality and range (ESR violation). The index is used for the predicate but can't deliver sorted output.
-
R03 Which ratio flags an inefficient scan in explain()?
totalDocsExaminedvsnReturned— examining thousands to return ten means the index isn't selective for this query shape. -
R04 executionStats vs queryPlanner mode — what's the difference?
queryPlanner shows the chosen (winning) plan without running it; executionStats actually executes and reports real counts and timings per stage.