How do I verify an index is being used in MongoDB?
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.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Mongoose Indexes: Types and When to Use Them
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.
Still have questions?
Browse all our FAQs or reach out to our support team
