01 / 18

What are indexes?

Difficulty: 5/10
index creation, query performance, index strategy

In MongoDB, indexes are data structures that store a small portion of the collection's data, enabling efficient query execution by allowing MongoDB to quickly locate documents without scanning the entire collection.

  1. 1

    Indexes support efficient execution of queries in MongoDB.

  2. 2

    Without indexes, MongoDB must scan every document in a collection to return query results.

  3. 3

    MongoDB uses the index to limit the number of documents it must scan.

Scenario Questions

0-2 years experience

  1. 1You need to retrieve all orders for a specific customer ID from a MongoDB collection. How would you use an index to make this query fast, and what command would you run to create it?
  2. 2If you run a query that filters on a field that doesn't have an index, what performance impact would you expect and why?

2-5 years experience

  1. 1Your team added a compound index on {status:1, createdAt:-1} but notice the query filtering only by status is still slow. What could be causing this, and how would you investigate?
  2. 2During a deployment you see a sudden increase in write latency after adding a new index. Explain why this might happen and how you would decide whether to keep the index.

5-8 years experience

  1. 1We have a sharded MongoDB cluster handling billions of documents. How would you design the indexing strategy to support range queries on a timestamp field while minimizing shard key imbalance?
  2. 2Explain the trade‑offs between using a wildcard index versus a set of targeted indexes in a logging service that stores heterogeneous documents.

8+ years experience

  1. 1Our legacy monolith stores audit logs in a single collection with a mix of fields. We need to migrate to a new microservice architecture with separate collections per log type. How would you plan the index migration to avoid downtime and ensure query performance throughout?
  2. 2Across multiple teams, there is a push to standardize index naming conventions and TTL policies. What governance model would you propose to balance flexibility, performance, and operational overhead?

Follow-up Questions

  • Can you walk me through how you would verify that the index is being used by a query?
  • What factors would lead you to drop an index you previously added?
  • How do you monitor index health and fragmentation over time?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.