• Join StackChief
  • Blog
  • Tutorials
  • Questions
  • React
  • JavaScript
  • MongoDB
  • NodeJs
  • Kafka
  • Java
  • Spring Boot
  • Examples

Blog

List Indexes | MongoDB Indexing Tutorial

Compound Index | MongoDB Indexing Tutorial

Index Performance | MongoDB Indexing Tutorial

Indexing Types | MongoDB Indexing Tutorial

Unique Index | MongoDB Indexing Tutorial

List Indexes | MongoDB Indexing Tutorial

Create Index | MongoDB Indexing Tutorial

Key Takeaways

  • Indexes in MongoDB improve query performance and allow efficient data retrieval.
  • Use getIndexes() method to list indexes for a specific collection.
  • A loop with getCollectionNames() allows you to list indexes for all collections in a database.

List indexes in a collection

db.getCollection('documents').getIndexes()

This command fetches all indexes for a specific collection, like your people collection. The result is an array of objects detailing each index:

[
  {
    "v": 2,
    "key": { "_id": 1 },
    "name": "_id_"
  },
  {
    "v": 2,
    "key": { "email": 1, "firstName": 1 },
    "name": "email_1"
  }
]

The array contains an entry for each index. Each object shows the index version (v), the fields used in the index (key), and the index name (name). The first entry is a simple index on the _id field, while the second is a compound index on email and firstName.

List indexes in entire DB

db.getCollectionNames().forEach(collection => {
  const indexes = db[collection].getIndexes();
  print(`Indexes for ${collection}:`);
  printjson(indexes);
});

This command iterates over all collections, printing out their indexes. It’s a quick way to review how your indexes are set up across collections:

Indexes for people:
[
  {
    "v": 2,
    "key": { "_id": 1 },
    "name": "_id_"
  },
  {
    "v": 2,
    "key": { "email": 1, "firstName": 1 },
    "name": "email_1"
  }
]
Indexes for places:
[
  {
    "v": 2,
    "key": { "_id": 1 },
    "name": "_id_"
  },
  {
    "v": 2,
    "key": { "location": 1 },
    "name": "location_1"
  }
]

You can quickly assess the structure and configuration of each collection’s indexes.

FAQ

Why are indexes important in MongoDB?

Indexes improve query performance by allowing the database to find and retrieve specific rows much faster than scanning the entire dataset. They are essential for efficiently accessing large databases.

Can I list indexes without using the mongo shell?

Yes, you can list indexes programmatically using MongoDB drivers for languages like Python, Node.js, etc. These libraries provide methods to access database collections and their indexes.

What happens if I don’t have indexes set up?

Without indexes, MongoDB will perform a full collection scan for each query, which can be very slow and resource-intensive, especially on large datasets.

What are compound indexes?

Compound indexes include multiple fields, allowing you to optimize queries that involve multiple elements. They can be used to improve performance for complex queries.

Mastering the tech interviewWhat everyone is doing wrong in tech interviews
Comment