1.What the query planner does
Before running a query, MongoDB's query planner considers every index that could help, runs the plausible candidates for a short trial, and picks whichever returned the most results for the least work. That choice is then cached and reused for future queries of the same shape.
Key features
- The planner generates candidate plans from every index that could apply to the query
- Candidates run in parallel for a short trial period; the plan producing the most results for the least work wins
- The winning plan is cached (in the query plan cache) and reused for future queries with the same shape
- MongoDB 8.3+ adds a Cost-Based Ranker as a backup mechanism, using cost/cardinality estimation rather than a trial run
- explain() bypasses the cache — it always evaluates plans fresh and does not create or consume a cache entry
- candidate plans — one per usable index
- trial run — most results, least work
- winning plan — cached for reuse
Command
db.<collection>.find({...}).explain("executionStats") // shows the winning plan, bypassing the cacheThe planner runs plausible candidate plans in a short trial, picks the one returning the most results for the least work, and caches that winner for future queries of the same shape. explain() always bypasses the cache.