06 / 18

How to list all indexes?

Difficulty: 2/10
Index management, MongoDB shell commands, Performance monitoring

To list all indexes in a MongoDB collection, you can use the following methods:

  1. 1

    db.collectionName.getIndexes();

  2. 2

    db.runCommand({ listIndexes: 'collectionName' });

  3. 3

    If you are using MongoDB Compass (a graphical user interface for MongoDB), navigate to the collection, and under the 'Indexes' tab, you can view all existing indexes.

Scenario Questions

0-2 years experience

  1. 1You need to verify which indexes exist on the 'users' collection before adding a new one. How would you list all indexes using the Mongo shell?
  2. 2If you run db.orders.getIndexes() and see an unexpected index, what steps would you take to confirm its definition?
  3. 3How can you list indexes for every collection in a database without writing a separate command for each?

2-5 years experience

  1. 1Your new feature is running slowly and you suspect missing or redundant indexes. Walk me through how you'd list indexes across the relevant collections and decide what to change.
  2. 2A deployment script fails because an index already exists. How would you programmatically check for existing indexes before attempting to create them?
  3. 3The query planner is using a wildcard index you didn't intend. How would you list the current indexes to investigate the issue?

5-8 years experience

  1. 1Design a monitoring tool that periodically audits index usage across hundreds of collections in a sharded cluster. How would you retrieve the list of indexes efficiently, and what performance or permission concerns would you address?
  2. 2When planning a migration to a newer MongoDB version, you need to ensure index compatibility. Describe how you'd enumerate all indexes, capture their definitions, and validate them against the target version.
  3. 3Your team wants to enforce a policy that no collection has more than 64 indexes. How would you programmatically list indexes across all databases and report any violations?

8+ years experience

  1. 1Multiple services share the same MongoDB deployment, and you need a governance process for index lifecycle. How would you design a system to catalog all indexes, track ownership, and coordinate deprecation across teams?
  2. 2During a multi‑year data platform redesign, you must migrate legacy collections with custom index naming conventions to a standardized scheme. Explain how you'd extract the current index list, map to new names, and apply changes with minimal downtime.
  3. 3Your organization is consolidating several MongoDB clusters into a single multi‑tenant cluster. How would you audit existing indexes across all tenants, identify duplicates, and plan a safe re‑indexing strategy?

Follow-up Questions

  • What would you do if the index list is huge and the command times out?
  • How do you differentiate between the built‑in _id index and user‑created ones?
  • Can you script this check to run as part of CI?
Share

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