What is an Index in MongoDB?
Learn what a MongoDB index is, how B-tree indexes avoid collection scans, which index types are available, and how to verify usage with explain().
Expected Interview Answer
An index in MongoDB is a separate, ordered data structure that stores a small copy of specified field values, allowing the database to locate matching documents quickly instead of scanning every document in a collection.
Without an index, MongoDB must perform a collection scan, examining every document to find matches, which becomes slow as data grows. Indexes are typically implemented as B-trees, sorted so that equality, range, and sort queries on the indexed field can jump directly to relevant entries. Every collection automatically has an index on `_id`, and you can create additional single-field, compound, multikey, text, or geospatial indexes as query patterns demand. Indexes speed up reads and sorts but add overhead to writes and consume additional storage, so they should be created deliberately based on real query patterns.
- Avoids full collection scans on common queries
- Speeds up equality, range, and sort operations
- Supports compound, multikey, text, and geospatial variants
- Enforces uniqueness when created as a unique index
- Query planner uses explain() to show if an index was used
AI Mentor Explanation
An index is like a stadium's alphabetical player directory at the entrance instead of walking every aisle to find one athlete. A steward jumps straight to the 'S' section for Smith rather than checking every seat, the same way MongoDB uses an index to jump to matching documents instead of scanning the whole collection.
Step-by-Step Explanation
Step 1
Without an index
MongoDB performs a collection scan, checking every document, which is slow on large collections.
Step 2
B-tree structure
Indexes are ordered B-tree structures storing indexed field values pointing back to their documents.
Step 3
Default _id index
Every collection automatically has a unique index on the _id field created at collection setup.
Step 4
Creating an index
Additional indexes are created with createIndex(), specifying single fields, compound fields, or special types.
Step 5
Verifying usage
explain() shows the query planner's chosen execution plan, confirming whether an index was actually used.
What Interviewer Expects
- Explains an index as a structure that avoids full collection scans
- Knows _id is indexed automatically by default
- Can describe compound, multikey, text, and geospatial index types
- Understands the write and storage overhead tradeoff
- Mentions explain() for verifying index usage
Common Mistakes
- Assuming MongoDB is schema-less so indexes are unnecessary
- Adding indexes for every field without checking query patterns
- Forgetting indexes slow down writes and use extra disk space
- Not verifying index usage with explain() before trusting performance
Best Answer (HR Friendly)
“An index in MongoDB is like a shortcut lookup table that lets the database find matching records quickly instead of checking every single record. It makes searches much faster, similar to how a book's index helps you find a topic without reading every page.”
Code Example
// Create a compound index on status and createdAt
db.orders.createIndex({ status: 1, createdAt: -1 });
// Verify the query planner uses it
db.orders.find({ status: "shipped" }).sort({ createdAt: -1 }).explain("executionStats");
// winningPlan.stage: "IXSCAN" indicates the index was used, not a COLLSCANFollow-up Questions
- What is the difference between a single-field and a compound index?
- How does a multikey index work on array fields?
- What tradeoffs come with adding more indexes to a collection?
- How do you check whether MongoDB used an index for a query?
- What is a covered query and how does it relate to indexes?
MCQ Practice
1. What happens when MongoDB queries a field with no index?
Without a relevant index, MongoDB scans every document in the collection to find matches.
2. Which index does every MongoDB collection have by default?
Every collection automatically gets a unique index on the _id field.
3. Which method shows whether a query used an index?
explain() reveals the query planner's execution plan, including whether an index scan or collection scan was used.
Flash Cards
Why use an index in MongoDB? — To avoid full collection scans and speed up equality, range, and sort queries.
What data structure backs most MongoDB indexes? — A B-tree, storing sorted field values pointing to their documents.
What index exists by default on every collection? — A unique index on the _id field.
What is the main cost of adding more indexes? — Slower writes and extra storage, since each index must be updated and stored.