Indexes in MongoDB are special data structures that store a small portion of your collection’s data in an easy-to-search format — making queries faster (just like an index in a book helps you find topics quickly without reading every page).
✅ 1. What is an Index?
Without an index, MongoDB performs a collection scan, checking every document — which is slow for large datasets.
With an index, MongoDB can jump directly to the documents that match your query, just like how a dictionary lets you find a word instantly.
✅ 2. Example
// Without index
db.users.find({ email: "john@gmail.com" });
// MongoDB scans every document
// Create an index on email
db.users.createIndex({ email: 1 });
// Now queries are much faster
db.users.find({ email: "john@gmail.com" });
Here, { email: 1 } creates an ascending index on the email field.
✅ 3. Types of Indexes
🟢 Single Field Index
db.users.createIndex({ username: 1 });
→ Speeds up queries filtering by one field.
🟢 Compound Index
db.users.createIndex({ firstName: 1, lastName: 1 });
→ Used when queries involve multiple fields together.
⚠️ Order matters: { firstName, lastName } ≠ { lastName, firstName }.
🟢 Text Index
db.articles.createIndex({ content: "text" });
db.articles.find({ $text: { $search: "React" } });
→ Used for full-text search on string content.
🟢 Unique Index
db.users.createIndex({ email: 1 }, { unique: true });
→ Prevents duplicate values (commonly used for emails or IDs).
🟢 TTL (Time-to-Live) Index
db.sessions.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 });
→ Automatically deletes documents after a certain time (e.g., for sessions or logs).
✅ 4. How Indexes Affect Performance
| Operation | Effect |
|---|---|
| Read Queries | ✅ Much faster, as only relevant documents are scanned. |
| Write Operations (Insert/Update/Delete) | ⚠️ Slightly slower because MongoDB must update indexes too. |
| Storage | ⚠️ Indexes consume extra disk space. |
| Sort Operations | ✅ Faster if sort field is indexed. |
✅ 5. Check Index Usage
You can see if a query is using an index:
db.users.find({ email: "john@gmail.com" }).explain("executionStats");
If you see "IXSCAN" → the query used an index.
If you see "COLLSCAN" → it scanned the entire collection (inefficient).
✅ 6. Best Practices
- Always index fields used in queries, filters, or sorts.
- Avoid too many indexes — they slow down writes.
- Use compound indexes carefully (match your most common query patterns).
- Regularly monitor and remove unused indexes using
db.collection.getIndexes().
✅ Example Summary
// Creating indexes
db.users.createIndex({ email: 1 }); // Single field
db.orders.createIndex({ userId: 1, date: -1 }); // Compound
db.logs.createIndex({ createdAt: 1 }, { expireAfterSeconds: 86400 }); // TTL
// Checking index usage
db.orders.find({ userId: "123" }).explain("executionStats");
In short:
Indexes in MongoDB make reads and queries faster by allowing efficient lookups, but they increase write overhead and storage — so design indexes based on your most common query patterns for the best performance.