Understand MongoDB Indexes
Indexes improve the efficiency of database queries by allowing MongoDB to locate matching documents without scanning the entire collection.
Without an appropriate index, a query may perform a collection scan.
For example, our application frequently searches users by email.
An index on email can improve this lookup.
MongoDB supports several index types:
- Single-field indexes.
- Compound indexes.
- Multikey indexes.
- Text indexes.
- Geospatial indexes.
- TTL indexes.
The correct index depends on the query patterns used by the application.
Common Index Types
Single-field
Indexes one field
Compound
Indexes multiple fields
Multikey
Indexes array values
Text
Supports text search
TTL
Automatically expires documents
Key Takeaways
Indexes improve query efficiency.
Choose indexes based on actual query patterns.
Indexes consume storage and add write overhead.