An index in MongoDB is a supplementary data structure that stores a sorted, compact representation of a collection's field values alongside pointers to the full documents on disk. Its sole purpose is to allow MongoDB to locate documents without scanning the entire collection.
Without an index, MongoDB performs a COLLSCAN (Collection Scan) — it reads every document. With an index, MongoDB performs an IXSCAN (Index Scan) — it traverses a B-Tree to find relevant pointers, then fetches only those documents.
| Aspect | COLLSCAN (No Index) | IXSCAN (With Index) |
|---|---|---|
| Method | Read every document | B-Tree traversal + fetch |
| Complexity | O(N) — linear | O(log N) — logarithmic |
| 5M documents | 5,000,000 reads | ~23 B-Tree comparisons |
| CPU & Disk | High | Minimal |
The _id Index
Every MongoDB collection has one index that is created automatically, is always in use, and can never be dropped: the unique ascending index on _id. All queries filtering by _id use this index for free.
// The _id index exists on every collection — you never need to create it db.users.getIndexes() // Always returns at minimum: [ { key: { _id: 1 }, name: "_id_", unique: true } ]