02 / 18

How to create Indexes?

Difficulty: 5/10
single-field indexes, compound indexes, sharding & index strategy

To create an index, use the createIndex() shell method or

In nodeJS:

Scenario Questions

0-2 years experience

  1. 1We have a collection of user profiles and we frequently query by email. How would you create an index to support that query in MongoDB?
  2. 2If you run a find on the 'orders' collection filtering on 'status' and notice it's scanning the whole collection, what index would you add and how would you verify it's being used?

2-5 years experience

  1. 1Our product team added a new feature that sorts blog posts by 'publishDate' and filters by 'authorId'. The query is slow. Walk me through how you'd design the appropriate index and what trade‑offs you consider.
  2. 2After deploying a new compound index on 'region' and 'price', some queries that only filter on 'price' got slower. Why might that happen and how would you address it?

5-8 years experience

  1. 1We have a sharded MongoDB cluster handling billions of documents. Explain how you would choose index keys to avoid hotspot writes and ensure balanced shard distribution.
  2. 2Our analytics pipeline runs aggregation pipelines that join large collections. Discuss the impact of index selection on pipeline performance and how you would monitor and adjust indexes at scale.

8+ years experience

  1. 1The company is migrating from a monolithic MongoDB deployment to a microservices architecture with separate databases per service. How would you create a unified index strategy that minimizes duplication, supports cross‑service reporting, and handles legacy data?
  2. 2Over several years, the schema has evolved and many indexes are now unused, causing write overhead. Describe a process for auditing, deprecating, and safely removing indexes across multiple production clusters.

Follow-up Questions

  • What command would you run to see if the index is actually being used?
  • How does adding an index affect write performance?
  • When would you prefer a sparse or TTL index over a regular one?
Share

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