04 / 18

What happens if I create an index on all fields of the collection?

Difficulty: 5/10
indexing, performance, storage

Indexes are data structures that occupy space and memory in database. Therefore, it is beneficial to create indexes for fields that are frequently used in query conditions/ filtering or for sorting, as this can significantly improve query performance.

Scenario Questions

0-2 years experience

  1. 1You have a small collection with 500 documents and you add an index on every field. What immediate impact would you notice when inserting new documents?
  2. 2If you query the collection by a field that already has an index, does having indexes on all other fields change the query performance?
  3. 3How would you verify that the indexes were created correctly after running createIndex on each field?

2-5 years experience

  1. 1Your team added indexes on all fields of a large collection (millions of docs) to improve search flexibility, but now writes are slow. Explain why this happened and what you would do to fix it.
  2. 2During a performance review you notice that a query using a compound filter is not using the expected index. How does having an index on every field affect the query planner's choice?
  3. 3You receive an alert that the storage size of the database grew unexpectedly after adding indexes on all fields. Walk through how you would investigate and mitigate the issue.

5-8 years experience

  1. 1Design a strategy for indexing a high‑traffic MongoDB collection that needs both fast reads on multiple fields and high write throughput. Would you ever consider indexing every field? Justify your decision with trade‑offs.
  2. 2Your service must support ad‑hoc filters on any field of a large collection. How would you balance the need for flexible queries against the overhead of indexing every field at scale?
  3. 3Explain how indexing every field impacts replica set replication lag and backup size, and propose mitigations for a production environment.

8+ years experience

  1. 1A legacy system has accumulated indexes on virtually every field across dozens of collections, causing operational headaches. As a staff engineer, outline a migration plan to refactor the indexing strategy while minimizing downtime.
  2. 2Across multiple teams, there is a policy to add an index for any field that might be queried. Discuss the long‑term architectural risks and how you would establish governance to prevent index bloat.
  3. 3If you were tasked with building a shared data platform that must serve both analytical and transactional workloads, how would you approach index management to avoid the pitfalls of indexing all fields?

Follow-up Questions

  • What metrics would you monitor to detect the impact of those indexes?
  • Can you describe how you would decide which indexes to keep or drop?
  • How does MongoDB's index prefix rule affect queries when many single‑field indexes exist?
Share

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