Question 31 / 50
Must KnowMediumConceptIndexing
How do indexes work in MongoDB, and why would you not index every field?
Think about it first.
Short answer
Indexes are B-trees from key values to document locations, so a query can seek instead of scanning. Every index is updated on write and wants RAM, so each one costs write throughput and memory.
Why?
If the working set of indexes does not fit in memory, lookups hit disk. Unused indexes are pure cost — $indexStats and dropping dead indexes are normal maintenance. Index the queries you actually run, not every field that exists.
Interview tip
The answer they want is the trade-off: faster reads paid for with slower writes and more memory.
Common mistake
Stopping at 'indexes make queries faster'.
How did you do?
Cheat Sheet