Why indexes exist
corebeginnerWithout an index, a query checks every document in the collection — a collection scan. An index is a sorted structure on one or more fields that lets MongoDB jump straight to matching values instead of checking each document in turn.
Think of it as
An index is like a book's index versus reading every page to find a topic: the sorted list of terms with page numbers lets you jump directly to what matters, at the cost of maintaining that list whenever the book (the collection) changes. The same trade applies to every index: faster reads, in exchange for extra work on every write that touches an indexed field.
What we're doing: Show the same query before and after an index, with explain() confirming the change from COLLSCAN to IXSCAN.
- 1
- Without an index, MongoDB has no shortcut — it examines every document to check whether its email field matches.
- 5
- After the index exists, the same query jumps directly to the matching entry, examining only the documents that actually match.
Why this works: explain() is what turns "indexes make queries faster" from a claim into something verifiable — the stage name and totalDocsExamined are the concrete, checkable evidence of what actually changed.
Assuming a query is fast because "there's an index on the collection," without checking it is the right index
Wrong
Better
What you see: A collection "has indexes" but a specific slow query still shows COLLSCAN in its explain() output.
Why: A collection scan happens per query, based on whether that query's filter fields match an existing index — the mere existence of some index on the collection says nothing about whether it helps a query on different fields.
- no index — COLLSCAN — check every document
- createIndex — a sorted structure
- IXSCAN — jump to matches
Scan vs. index, at a glance
Together
Remember: No index means a collection scan (COLLSCAN) — every document checked. An index (IXSCAN) lets MongoDB jump to matches, at the cost of extra work on every write.
See also: single field indexes · compound indexes

