Why are indexes needed?
Indexes exist to speed up searching and filtering data in a collection.
At a junior level, the gist in 3 points:
- Fast search Instead of scanning every document, the database finds the right ones through the index right away.
- Query optimization
Filters, sorting, and
find()run faster, especially on large collections. - Less load on the server Queries use fewer resources and run faster.
An analogy
An index for a database is like a "table of contents in a book". Instead of flipping through the whole book, you jump straight to the page you need.
Summary: indexes make queries faster and more efficient, especially when there's a lot of data.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.