The command syntax is as shown in the following:
{
listIndexes: "<collection-name>",
cursor: { batchSize: <int> },
comment: <any>
}
)
The supported parameters are as follows:
- collection_name – Specifies the name of the target collection as a string type.
- batchSize – Specifies the batch size for the cursor.
- comment – User comment for the specified command.
Consider the following example query:
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:
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:
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!