MongoDB — Indexes
Create Index
db.users.createIndex({ email: 1 })
Unique Index
db.users.createIndex({ email: 1 }, { unique: true })
Compound Index
db.users.createIndex({ country: 1, age: -1 })
List Indexes
db.users.getIndexes()
Drop Index
db.users.dropIndex({ email: 1 })
Index Types
| Type | Description |
|---|---|
| Single | One field |
| Compound | Multiple fields |
| Unique | No duplicates |
| Text | Full-text search |
| Geospatial | Location data |
| TTL | Auto-expire |
Index Properties
| Property | Description |
|---|---|
| unique | No duplicate values |
| sparse | Skip null values |
| expireAfterSeconds | TTL index |
Mini Practice
- Create basic index
- Create unique index
- Create compound index
- Check index usage
Up Next
Continue with Text Search — full-text search.
Related Topics
Frequently Asked Questions about Indexes
What is Indexes in MongoDB?
Indexes is a fundamental concept in MongoDB. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Indexes?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Indexes.
Why is Indexes important in MongoDB?
Indexes is essential for MongoDB development. Understanding this concept will help you write better code and solve real-world problems more effectively.