Indexes allow the MongoDB to quickly fetch the documents that match a specific query without scanning the entire collection for matching results.
However, it is good to keep in mind that this post does not cover the process of index creation. Check our tutorial on the topic to learn more.
Let’s jump in.
Method 1: MongoDB getIndexes() Method
The first method that we will discuss is the getIndexes() method. This method is available in MongoDB Shell which allows you to get all the indexes in a given collection as an array.
This method returns a detailed information about the available indexes, including the hidden indexes in a human-readable format.
The command syntax is as shown in the following:
NOTE: Depending on the cluster configuration, this method may require the running user to have the List Indexes permissions on the target collection.
Suppose we have a collection called “netflix”. We can get all the indexes in the collection by running the following query:
The request should return an array with the information about the available indexes in the target collection.
An example output is as shown in the following:
{ v: 2, key: { _id: 1 }, name: '_id_' },
{
v: 2,
key: { year: 1, score: -1 },
name: 'year_index',
collation: {
locale: 'en',
caseLevel: false,
caseFirst: 'off',
strength: 3,
numericOrdering: false,
alternate: 'non-ignorable',
maxVariable: 'punct',
normalization: false,
backwards: false,
version: '57.1'
}
}
]
In this case, the collection contains one index called “year_index”. We can also find another index information such as the strength, version, locale, and more.
Method 2: Get the Collection Indexes Using listIndexes Method
The second method that we can use to view the list of available indexes in a given collection is the listIndexes method. This method is a database command and is supported in a language specific driver.
The command syntax is as shown:
{
listIndexes: "<collection-name>",
cursor: { batchSize: <int> },
comment: <any>
}
)
The supported parameters are as shown:
- 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 previous query invokes the listIndexes command on the “netflix” collection. This should return all the indexes for the specific collection as shown in the following output:
cursor: {
id: Long("0"),
ns: 'entertainment.netflix',
firstBatch: [
{ v: 2, key: { _id: 1 }, name: '_id_' },
{
v: 2,
key: { year: 1, score: -1 },
name: 'year_index',
collation: {
locale: 'en',
caseLevel: false,
caseFirst: 'off',
strength: 3,
numericOrdering: false,
alternate: 'non-ignorable',
maxVariable: 'punct',
normalization: false,
backwards: false,
version: '57.1'
}
}
]
},
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1663940670, i: 2 }),
signature: {
hash: Binary(Buffer.from("c243619d64ca61a5c651b4bbb21a8b020a101278", "hex"), 0),
keyId: Long("7110175001109594117")
}
},
operationTime: Timestamp({ t: 1663940670, i: 2 })
}
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("3645642518793960930"),
ns: 'entertainment.netflix',
firstBatch: [ { v: 2, key: { _id: 1 }, name: '_id_' } ]
},
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1663940831, i: 2 }),
signature: {
hash: Binary(Buffer.from("66058e9b4b4d59de21ff01c05480d84438e99411", "hex"), 0),
keyId: Long("7110175001109594117")
}
},
operationTime: Timestamp({ t: 1663940831, i: 2 })
}
You can increase the batchSize to get more information about the indexes in the collection.
Conclusion
In this post, we discussed the two main methods of fetching all the indexes of a given collection using the MongoDB Shell commands and the Database commands. Feel free to explore the document for more detailed information.
Happy coding!