If your MongoDB query is slow, someone has probably told you: Just run .explain(). But what does it actually mean? And why is it complex to interpet?
Let’s simplify what .explain() does, why it matters, and how to finally make sense of it.
What Does .explain() do?
When you run a query like this:
db.orders.find({ customerId: 123 }).explain()
MongoDB doesn’t execute the query normally. Instead, it returns a detailed plan showing:
- How it plans to fetch the documents
- Whether it will use an index or scan the whole collection
- How many documents it needs to read, filter, and return
This helps you understand performance problems especially if queries are slow or resource-hungry.
explain() output is brutal:
Here’s a typical .explain("executionStats") output (just a slice of it):
{
"stage": "COLLSCAN",
"nReturned": 8,
"executionTimeMillis": 219,
"totalDocsExamined": 1000000,
...
}
The COLLSCAN tells you MongoDB is scanning every single document in the collection, bad sign!
But let’s be honest: even for experienced devs, this raw JSON is hard to read, noisy, and often overlooked. Now imagine trying to spot that buried in 200 lines of JSON every time.
The .explain() output shows how MongoDB plans to fetch your data. Here are the fields that actually matter:
| Field | What it tells you |
|---|---|
| stage | The type of operation (eg. COLLSCAN, IXSCAN) |
| nReturned | How many documents matched your query |
executionTimeMillis | How long it took (in ms) |
totalDocsExamined | How many docs MongoDB had to look at |
totalKeysExamined | How many index entries were checked |
📚 See full reference in the official docs
Visual .explain() could make it simpler:
Mongo Pilot, a smart MongoDB GUI, makes .explain() easier and actionable. When you run a query, you can instantly:
- Instant feedback on whether indexes are used
- Compare execution time and documents examined
- A visual query plan (no more scanning JSON trees)

So in the above example, we directly have the key information. This query was run a big collection and didn’t use an index. Took 19s to run, and the ratio of Docs Examined/ Docs Returned is showing a performance issue. Mongo Pilot interface also gives the raw JSON output of the .Explain() query. One click to copy it and share with colleagues.
Download Mongo Pilot Now🧪 Bonus: Benchmark Indexes Before Production
Mongo Pilot also lets you:
- Create and test new indexes without deploying them on your current collection
- Compare a query performance with and without an index
- View live query stats (not guesses)
Perfect for developers who want to test before they touch prod.
Also, .explain() also works with MongoDB aggregation pipelines to better understand the performance.
Read also > How to optimize $stage performance in Aggregation Pipeline
Test your MongoDB skills by talking our “MongoDB Quiz Challenge“
TL;DR
.explain()tells you how MongoDB runs your query
- Key fields:
stage,executionTimeMillis,totalDocsExamined.
- If you see
COLLSCAN= bad. You wantIXSCAN.
Mongo Pilot makes .explain() easy to read and act on no CLI and JSON stress.
👉 Try it now in Mongo Pilot GUI for faster insights
