Indexing for MongoDBs
MongoDB indexing creates ordered data structures, usually B-trees, so queries find documents without scanning a whole collection. Index types include single field, compound, multikey, text, geospatial, TTL and partial. This unit in the Performance stage teaches how to create them and verify their use through explain plans.
Stage: Performance · Part of the MongoDB roadmap · Last reviewed 19 November 2026
What to learn in Indexing
- Create single-field and compound indexes with createIndex
- Order compound index fields using the equality, sort, range guideline
- Read explain output to tell IXSCAN from COLLSCAN
- Add a TTL index to expire documents automatically
- Identify unused or redundant indexes that slow writes
Indexing interview questions
How do you check whether a MongoDB query uses an index?
Run explain('executionStats') on the query. A winning plan with IXSCAN means an index is used, while COLLSCAN means a full scan. Compare totalDocsExamined and nReturned, because a large gap shows the index is not selective enough.
Does the order of fields in a compound index matter?
Yes. A compound index supports queries that use a leftmost prefix of its fields, and its order affects sorting. A common rule is to put equality fields first, then sort fields, then range fields, matching how the query filters and sorts.
Where Indexing fits in the MongoDB roadmap
Indexing belongs to the Performance stage (intermediate level), which takes about 12 hours. Indexes and aggregation.
Other units in this stage
Back to the full MongoDB roadmap