Reference · Cheat sheet
Lesson 0004 distilled — Mongo races plans instead of costing them; read the stage tree and the examined:returned ratio. Built to print.
The classic planner races candidate plans in a short trial and keeps the one with most results, least work — an empirical run-off, not a cost-from-stats estimate.
(8.3+ adds a cost-based ranker as a backup.)
queryPlanner (default) — intended plan, doesn't run.
executionStats — runs it, real counts. Use this to tune.
allPlansExecution — adds losers' trial data.
COLLSCAN = read every doc (bad). IXSCAN = walk index keys.
FETCH = go get the document. SORT = in-memory sort (index didn't supply order).
IXSCAN + no FETCH = covered query. winningPlan / rejectedPlans.
nReturned vs totalKeysExamined vs totalDocsExamined.
Near 1:1 = efficient. Big gap = reading lots to emit little (wrong/broad index or COLLSCAN).
Cousin of Postgres estimated-vs-actual rows.
Winner cached by query shape, reused. States: Missing → Inactive → Active.
Cleared by any DDL (create/drop/hide index), LRU, restart.
explain() bypasses the cache and creates no entry.
Choose plan: cost-from-stats → race. #1 tell: est-vs-actual → examined-vs-returned.
Real numbers: EXPLAIN ANALYZE → explain("executionStats") (and it's safe on writes).