Indexes
Types of Indexes
What are Indexes in MongoDB?
Indexes are used to improve the speed of queries.
Without index → MongoDB scans every document (slow)
With index → MongoDB quickly finds data (fast)
Think of it like:
Index in a book → helps you find topics quickly instead of reading every page
🔹 Why Indexes are Important
- Faster data retrieval
- Efficient searching
- Improves performance for large datasets
🔹 How Index Works
MongoDB creates a data structure (like a sorted list)
Example:
If you index age, MongoDB stores sorted values → 20, 25, 30, 35
So searching becomes very fast
🔹 Create an Index
db.users.createIndex({ age: 1 })
1 → ascending
-1 → descending🔹 Types of Indexes
1. Single Field Index
Index on one field
db.users.createIndex({ age: 1 })2. Compound Index
Index on multiple fields
db.users.createIndex({ age: 1, name: 1 })
Used when queries involve multiple fields3. Unique Index
Ensures values are unique
db.users.createIndex({ email: 1 }, { unique: true })
Prevents duplicate values4. Text Index
Used for searching text
db.users.createIndex({ name: "text" })
Useful for search features5. Multikey Index
Used for arrays
Automatically created when indexing array fields
🔹 Default Index
MongoDB automatically creates an index on:
_id (ObjectId)
Ensures every document is unique
🔹 Check Indexes
db.users.getIndexes()🔹 Drop Index
db.users.dropIndex({ age: 1 })
🔹 When to Use Indexes
Use indexes when:
- You frequently search a field
- You use sorting
- You filter large data
Avoid too many indexes because:
- They take memory
- Slow down insert/update
Simple Understanding
- Index = speed booster
- More indexes = faster reads but slower writes
🔹 What are Special Indexes?
Special indexes are advanced indexes designed for specific use cases, beyond normal single or compound indexes.
🔹 1. Text Index
Used for searching text content
Example:
db.posts.createIndex({ title: "text", content: "text" })Supports keyword search like:
- search articles
- search products
Used in: blogs, search features
🔹 2. Geospatial Index
Used for location-based queries
Types:
- 2d → flat coordinates
- 2dsphere → real-world earth coordinates
Example:
- Find nearby restaurants
- Delivery apps
🔹 3. TTL Index (Time-To-Live)
Automatically deletes documents after a certain time
Example:
db.sessions.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 })
Document will be deleted after 1 hour
Used in:
- session data
- cache data
🔹 4. Sparse Index
Indexes only documents that contain the field
Example:
db.users.createIndex({ email: 1 }, { sparse: true })
Documents without email are ignored🔹 5. Partial Index
Indexes only documents that match a condition
Example:
db.users.createIndex(
{ age: 1 },
{ partialFilterExpression: { age: { $gt: 18 } } }
)
Only users with age > 18 are indexed🔹 6. Hashed Index
Used for sharding and equal distribution
Example:
db.users.createIndex({ userId: "hashed" })
Good for:
distributed systems
balancing load🔹 7. Wildcard Index
Indexes all fields or dynamic fields
Example:
db.products.createIndex({ "$**": 1 })
Useful when schema is flexibleSimple Understanding
- Text → search words
- Geo → location queries
- TTL → auto delete
- Sparse → ignore missing fields
- Partial → filter-based index
- Hashed → distribution
- Wildcard → dynamic fields