MongoDB

MongoDB List All Indexes for a Database

We can use the listIndexes method to fetch all the indexes in a given collection. This method is a database command and is supported in language specific driver.

The command syntax is as shown in the following:

db.runCommand (
   {
      listIndexes: "<collection-name>",
      cursor: { batchSize: <int> },
      comment: <any>
   }
)

The supported parameters are as follows:

  1. collection_name – Specifies the name of the target collection as a string type.
  2. batchSize – Specifies the batch size for the cursor.
  3. comment – User comment for the specified command.

Consider the following example query:

db.runCommand({listIndexes: "hulu"})

The given query invokes the listIndexes command on the “hulu” collection. This should return all the indexes for the specific collection as shown in the following output:

{
  cursor: {
    id: Long("0"),
    ns: 'entertainment.hulu',
    firstBatch: [
      { v: 2, key: { _id: 1 }, name: '_id_' },
      {
        v: 2,
        key: { release_year: 1 },
        name: 'release_year_index',
        sparse: false
      }
    ]
  },
  ok: 1
}

Since we do not specify the batch size for the cursor, the command returns all the results of the listIndexes command.

To show only one result, we can set the batchSize parameter to 1 as shown in the following:

db.runCommand({listIndexes: "hulu", cursor: {batchSize: 1}})

In this case, the command should return an output as shown in the following:

{
  cursor: {
    id: Long("6609474838031556340"),
    ns: 'entertainment.hulu',
    firstBatch: [ { v: 2, key: { _id: 1 }, name: '_id_' } ]
  },
  ok: 1
}

You can increase the batchSize to get more information about the indexes in the collection.

MongoDB Show All Indexes in a Database

Suppose we wish to show all the indexes in all the collections of a specified database. For that, we can use the forEach function to iterate over each collection in the database and list its corresponding indexes.

An example query is as shown in the following:

db.getCollectionNames().forEach(function (collection) { all_indexes = db.getCollection(collection).listIndexes(); print("Indexes of " + collection + ":->"); printjson(all_indexes); });

The previous query starts by fetching all the collection names in a given database using the getCollectionNames() method. We then iterate over each collection in the database and pass them as parameters of the getIndexes() method.

Finally, we save each result into the all_indexes variable. To get a human readable output, we use the print and printjson methods for better formatting.

This should return all the collections and their corresponding indexes as shown in the following:

[
  { v: 2, key: { _id: 1 }, name: '_id_' },
  {
    v: 2,
    key: { _fts: 'text', _ftsx: 1 },
    name: 'rating',
    sparse: false,
    weights: { rating: 1 },
    default_language: 'english',
    language_override: 'language',
    textIndexVersion: 3
  }
]
Indexes of amazon_prime:->
[
  { v: 2, key: { _id: 1 }, name: '_id_' },
  {
    v: 2,
    key: { _fts: 'text', _ftsx: 1 },
    name: 'content_type',
    sparse: false,
    weights: { type: 1 },
    default_language: 'english',
    language_override: 'language',
    textIndexVersion: 3
  }
]
Indexes of hulu:->
[
  { v: 2, key: { _id: 1 }, name: '_id_' },
  {
    v: 2,
    key: { release_year: 1 },
    name: 'release_year_index',
    sparse: false
  }
]

In our example, we have three collections, each with one index as shown in the previous output array.

Conclusion

In this post, we discussed how to use the listIndexes() method to get the list of all indexes in a given collection. We also discussed how to create a simple script to get all the indexes and their associated collection in a given database.

Happy coding!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list