100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
MongoDB

Indexes in MongoDB

Learn how MongoDB indexes work internally, why they speed up queries, and how to create and manage the core index types.

Indexing & PerformanceBeginner9 min readJul 10, 2026
Analogies

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.

javascript
// 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

Was this page helpful?

Topics covered

#MongoDB#MongoDBStudyNotes#Database#IndexesInMongoDB#Indexes#Exist#Creating#Single#SQL#StudyNotes#SkillVeris