MongoDB Indexes Interview Questions and Answers
Common MongoDB index interview questions with concise answers.
MongoDB Indexes Interview Questions and Answers
Interviewers ask these MongoDB index questions often. Here are concise answers.
Q1: What is an index in MongoDB?
A data structure that speeds up queries on a field. Without an index, MongoDB scans every document. With an index, MongoDB looks up the field in the index and fetches only matching documents.
Q2: What is a compound index?
An index on two or more fields. MongoDB can use it for queries on any prefix of the indexed fields. Order matters: put the equality field first, then sort, then range (ESR rule).
Q3: What is the index prefix rule?
A compound index on (A, B, C) can be used for A, A+B, or A+B+C. It cannot be used for B, C, or B+C. If you query on B+C, you need a separate index on (B, C).
Q4: How do you verify an index is being used?
Run .explain('executionStats'). Look at winningPlan.stage (IXSCAN is good, COLLSCAN is bad) and executionStats.totalDocsExamined (should be small).
Q5: What is a unique index?
An index that enforces uniqueness. Duplicate inserts fail with a 11000 error. Use for email, username, and other fields that must be unique.
Q6: What is a text index?
An index for full-text search. schema.index({ title: 'text', body: 'text' }). Then query with { $text: { $search: 'nodejs' } }. Heavy; use sparingly.
Q7: What is a TTL index?
An index that auto-deletes documents after a timeout. schema.index({ createdAt: 1 }, { expireAfterSeconds: 3600 }). Use for sessions, tokens, or logs that should expire.
Q8: Why do indexes slow down writes?
Because MongoDB has to update each index on every insert, update, and delete. Indexes are a tradeoff: faster reads, slower writes. Index only the queries you actually run.
Q9: What is a covered query?
A query where all the fields you need are in the index. MongoDB returns results from the index without fetching the document. Fastest. Use .select() to limit fields and ensure the index covers them.
Q10: How do you optimize a slow MongoDB query?
Run explain(). If COLLSCAN or high totalDocsExamined, add an index for the filter fields. Use compound indexes for multi-field queries. Use .lean() and .select(). Paginate with cursor instead of skip. Avoid $where and unindexed $regex.
The Takeaway
MongoDB index interview answers: index speeds up queries; compound index for multi-field queries; prefix rule (A, B, C serves A, A+B, A+B+C); verify with explain(); unique index for uniqueness; text index for search; TTL for auto-expire; indexes slow writes; covered query is fastest; optimize with explain() and indexes.
An index on two or more fields. MongoDB can use it for queries on any prefix of the indexed fields. Order matters: equality first, then sort, then range (ESR rule).
A compound index on (A, B, C) can be used for A, A+B, or A+B+C. It cannot be used for B, C, or B+C. If you query on B+C, you need a separate index on (B, C).
Run .explain('executionStats'). Look at winningPlan.stage (IXSCAN is good, COLLSCAN is bad) and executionStats.totalDocsExamined (should be small, close to the number of documents returned).
Because MongoDB has to update each index on every insert, update, and delete. Indexes are a tradeoff: faster reads, slower writes. Index only the queries you actually run; do not index every field.
A query where all the fields you need are in the index. MongoDB returns results from the index without fetching the document. Fastest. Use .select() to limit fields and ensure the index covers them.
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.

