π MongoDB Indexing Cheatsheet
Complete guide to indexes, performance optimization, and best practices with live examples
What Are Indexes?
Why indexes are critical for performance
Imagine trying to find the word "MongoDB" in a 1000-page book. Without an index, you'd have to read every single page. With an index, you flip to the back, find "MongoDB" in the alphabetical index, and jump directly to page 547. That's exactly what database indexes do!
β’ Examines every document
β’ Slow for large collections
β’ Direct document access
β’ Scales with data size
Index Types Overview
All MongoDB index types explained
| Index Type | Description | Use Case | Example |
|---|---|---|---|
| Single Field | Index on one field | Simple queries on one field | { email: 1 } |
| Compound | Index on multiple fields | Queries on multiple fields | { age: 1, city: 1 } |
| Multikey | Index on array fields | Queries on array elements | { tags: 1 } |
| Text | Full-text search index | Search in text fields | { content: "text" } |
| Geospatial | Location-based index | Near, within queries | { location: "2dsphere" } |
| Hashed | Hash-based index | Sharding, equality matches | { _id: "hashed" } |
| TTL | Auto-expire documents | Sessions, temporary data | { createdAt: 1 } + TTL |
Single Field Indexes
Most common index type
Compound Indexes
Multi-field indexes for complex queries
For optimal compound indexes, follow the ESR Rule:
- Equality - Fields with exact matches first
- Sort - Fields used in sorting next
- Range - Fields with range queries last
Text Search Indexes
Full-text search capabilities
- Only ONE text index per collection
- Cannot be used with hint() for other queries
- Slower than regular indexes for exact matches
- For advanced search, consider Atlas Search
Geospatial Indexes
Location-based queries
- Always
[longitude, latitude]- NOT latitude first! - Longitude range: -180 to 180
- Latitude range: -90 to 90
- Distances in meters by default
Indexing Strategies
Best practices and patterns
- Fields used frequently in queries
- Fields used in sorting
- Fields used for uniqueness constraints
- Fields in join operations ($lookup)
- High cardinality fields (many unique values)
- Low cardinality fields (e.g., gender: M/F)
- Fields rarely queried
- Small collections (< 1000 docs)
- Write-heavy collections (indexes slow writes)
- Fields with frequent updates
High Selectivity (Good): Field with many unique values (email, username, SSN)
Low Selectivity (Bad): Field with few unique values (gender, boolean flags)
β’ email field: 10,000 unique values / 10,000 docs = 100% selectivity β
β’ gender field: 2 unique values / 10,000 docs = 0.02% selectivity β
Performance Optimization
Analyze and improve query performance
- Index frequently queried fields - Speed up reads
- Use compound indexes wisely - Follow ESR rule
- Create covered queries - Read from index only
- Monitor with explain() - Watch for COLLSCAN
- Drop unused indexes - Improve write speed
- Avoid indexes on low-selectivity fields - Waste of space
- Use partial indexes - Index subset of documents
- Background index builds - Don't block writes
- Regular index maintenance - Rebuild fragmented indexes
- Test before production - Always verify performance