Imagine you have a huge phone book and you want to find a person's phone number. Without an index, you might have to look at every page. How does an index help MongoDB find data faster?
Think about how a book's index helps you find a topic quickly.
Indexes create a fast lookup structure that points to the exact location of data, avoiding full collection scans.
Given a collection users with 100,000 documents, 10,000 have age: 30. You run this query:
db.users.find({age: 30}).count()What will be the count returned?
The count is about how many documents match, not how fast the query runs.
The query counts all documents where age is 30, which is 10,000 regardless of indexing.
You want to create an index on the email field in the users collection. Which command is correct?
Indexes need a field name and sort order inside an object.
The correct syntax uses an object with the field name as key and 1 for ascending order.
You often query orders collection filtering by customerId and status. Which index improves performance best?
Think about how combining fields in one index helps multi-field queries.
A compound index on both fields lets MongoDB efficiently filter by both at once.
Collection users has an index on username. This query is slow:
db.users.find({username: /john/i})Why?
Think about how indexes work with regular expressions.
Case-insensitive regex disables index use because it can't match index order efficiently.