Mongoose Indexes: Types and When to Use Them
Indexes make MongoDB queries fast. Here is which types exist and when to use each.
Mongoose Indexes: Types and When to Use Them
Without indexes, MongoDB scans every document in a collection. Indexes make queries fast. Here is which types exist and when to use them.
1. Single Field Index
Index on one field. Use for fields you query often.
userSchema.index({ email: 1 });
2. Unique Index
Enforces uniqueness. Use for fields that must be unique (email, username).
email: { type: String, unique: true }
Or:
userSchema.index({ email: 1 }, { unique: true });
3. Compound Index
Index on multiple fields. Use for queries that filter on multiple fields together.
connectionRequestSchema.index({ fromUserId: 1, toUserId: 1 }, { unique: true });
Order matters: put the field you filter by first, then the field you sort by. Use explain() to verify queries use the index.
4. Text Index
For full-text search. Use sparingly; it is heavy.
postSchema.index({ title: 'text', body: 'text' });
Then Post.find({ $text: { $search: 'nodejs' } }).
5. Geospatial Index
For location queries. Use 2dsphere for geo points.
placeSchema.index({ location: '2dsphere' });
6. TTL Index
Auto-deletes documents after a timeout. Use for sessions, tokens, or logs.
sessionSchema.index({ createdAt: 1 }, { expireAfterSeconds: 3600 });
When to Index
- You query by a field often.
- You sort by a field often.
- You need uniqueness (use unique index).
- You do full-text search (text index).
When Not to Index
- The collection is small (< 1000 docs).
- You rarely query by that field.
- Writes are very frequent (every index slows writes).
How to Verify an Index Is Used
User.find({ email }).explain('executionStats'). Look at executionStats.totalDocsExamined. If it equals the collection size, the index is not used.
Indexes and Write Performance
Every index adds overhead to writes. Indexes are a tradeoff: faster reads, slower writes. Do not index every field. Index the queries you actually run.
The Takeaway
Mongoose indexes: single field, unique, compound, text, geospatial, and TTL. Use them for fields you query and sort by, for uniqueness, for full-text, for geo, and for auto-expiring data. Do not index every field; index the queries you run. Verify with explain().
Single field, unique, compound, text (full-text), geospatial (2dsphere), and TTL (auto-expire). Each suits a different query pattern or constraint.
When you query on multiple fields together. For example, (fromUserId, toUserId) for connection requests. Put the field you filter by first, then the field you sort by. Use explain() to verify the index is used.
Every index adds overhead to writes because MongoDB has to update each index. Indexes are a tradeoff: faster reads, slower writes. Do not index every field; index only the queries you actually run.
An index with expireAfterSeconds that auto-deletes documents after a timeout. Use for sessions, tokens, or logs that should expire. Example: schema.index({ createdAt: 1 }, { expireAfterSeconds: 3600 }).
Call .explain('executionStats') on your query. Look at executionStats.totalDocsExamined. If it equals the collection size, the index is not used. If it is small, the index is working.
Ready to master Node.js completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master Node.js
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

