Why Indexes Exist
Without an index, MongoDB must perform a collection scan (COLLSCAN), reading every document in a collection to find the ones that match a query filter. This is fine for a few hundred documents but becomes catastrophically slow at millions of documents. An index is a separate, ordered data structure — a B-tree — that stores a small, sorted copy of one or more fields plus a pointer back to the full document, so the database can jump directly to matching records instead of scanning everything.
Cricket analogy: Searching for every Sachin Tendulkar century without an index is like a scorer flipping through every Test match scorecard since 1989 one by one; an index is the pre-built 'centuries by player' ledger that lets you jump straight to his 51 entries.
Creating a Single-Field Index
The most common index type covers a single field, created with db.collection.createIndex({ field: 1 }) where 1 means ascending order and -1 means descending. MongoDB automatically creates a unique index on _id for every collection, but any other field used frequently in query filters, sorts, or joins ($lookup) should be considered for an explicit index. Ascending versus descending order rarely matters for a single-field index since MongoDB can traverse a B-tree in either direction, but it matters greatly once you combine fields in a compound index.
Cricket analogy: Creating db.matches.createIndex({ venue: 1 }) is like Cricinfo building a 'sort by venue' index on its scorecards, so you can pull every match played at Eden Gardens without scanning the whole archive.
// Create an ascending single-field index on the "email" field
db.users.createIndex({ email: 1 }, { unique: true });
// Create a descending index for sorting recent orders first
db.orders.createIndex({ createdAt: -1 });
// List all indexes on a collection
db.orders.getIndexes();
// Drop an index by name
db.orders.dropIndex("createdAt_-1");MongoDB limits a single collection to 64 indexes by default, and every index adds write overhead (each insert/update must also update the index structures) and consumes RAM in the WiredTiger cache. Index deliberately, not exhaustively.
Multikey and TTL Indexes
When you index a field that holds an array, MongoDB automatically creates a multikey index, generating one index entry per array element so a query can match any single element without scanning the whole array. A TTL (time-to-live) index is a special single-field index on a date field that automatically deletes documents once a specified number of seconds has elapsed since that date, useful for session tokens, logs, or caches. TTL deletion runs as a background job roughly every 60 seconds, so expiry is approximate rather than millisecond-precise.
Cricket analogy: Indexing an array field like { playerRoles: 1 } that stores ['batsman', 'wicketkeeper'] for each player is a multikey index, letting you find every wicketkeeper instantly even though the field holds multiple roles per document.
TTL indexes only work on fields storing BSON Date values (or arrays of dates, using the earliest date). They cannot be combined with compound-index fields other than the date field, and expiry timing is not guaranteed to the second — do not rely on TTL for strict compliance deadlines.
- Without an index, MongoDB performs a COLLSCAN, reading every document — indexes use a B-tree to jump directly to matches.
- db.collection.createIndex({ field: 1 }) creates an ascending single-field index; -1 is descending.
- Every collection automatically has a unique index on _id.
- Indexing an array field automatically produces a multikey index with one entry per array element.
- TTL indexes on Date fields automatically delete documents after a configured number of seconds, checked roughly every 60 seconds.
- Indexes speed up reads but add overhead to every write and consume RAM, so index deliberately.
- A collection can have up to 64 indexes by default.
Practice what you learned
1. What happens when MongoDB queries a collection with no relevant index?
2. Which index type is automatically created when you index a field containing an array?
3. What data type must a TTL index field hold?
4. Which field does every MongoDB collection index by default?
5. Roughly how often does the MongoDB TTL background job check for expired documents?
Was this page helpful?
You May Also Like
Compound Indexes
Understand how MongoDB compound indexes combine multiple fields, the ESR rule for ordering keys, and how they support sorting and covered queries.
The Query Planner and explain()
Learn how MongoDB's query planner selects an execution plan and how to use explain() to diagnose slow queries.
Text and Geospatial Indexes
Learn how MongoDB's text indexes power keyword search and how 2dsphere geospatial indexes enable location-based queries.
Performance Tuning Basics
Practical techniques for diagnosing and fixing slow MongoDB queries, covering the profiler, index hygiene, schema design, and working-set memory.