Docs Menu
Docs Home
/
MongoDB Manual
/ / /

listIndexes

On this page

  • Definition
  • Compatibility
  • Syntax
  • Required Access
  • Behavior
  • Output
  • Examples
listIndexes

Returns information about the indexes on the specified collection. Returned index information includes the keys and options used to create the index, as well as hidden indexes. You can optionally set the batch size for the first batch of results.

Tip

In mongosh, this command can also be run through the db.collection.getIndexes() helper method.

Helper methods are convenient for mongosh users, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.

This command is available in deployments hosted in the following environments:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

Note

This command is supported in all MongoDB Atlas clusters. For information on all commands, see Unsupported Commands.

The command has the following form:

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

listIndexes takes the following fields:

Field
Type
Description
listIndexes
string
The name of the collection.
cursor.batchSize
integer
Optional. Specifies the cursor batch size.
comment
any

Optional. A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:

A comment can be any valid BSON type (string, integer, object, array, etc).

If access control is enforced, the built-in read role provides the required privileges to run listIndexes for the collections in a database.

Starting in MongoDB 4.2, if the client that issued listIndexes disconnects before the operation completes, MongoDB marks listIndexes for termination using killOp.

To run on a replica set member, listIndexes operations require the member to be in PRIMARY or SECONDARY state. If the member is in another state, such as STARTUP2, the operation errors.

Starting in MongoDB 5.0.16, the wildcardProjection field stores the index projection in its submitted form. Earlier versions of the server may have stored the projection in a normalized form.

The server uses the index the same way, but you may notice a difference in the output of the listIndexes and db.collection.getIndexes() commands.

listIndexes.cursor

A result set returned in the batch size specified by your cursor. Each document in the batch output contains the following fields:

Field
Type
Description
id
integer
A 64-bit integer. If zero, there are no more batches of information. If non-zero, a cursor ID, usable in a getMore command to get the next batch of index information.
ns
string
The database and collection name in the following format: <database-name>.<collection-name>
firstBatch
document

Index information includes the keys and options used to create the index. The index option hidden is only present if the value is true.

Use getMore to retrieve additional results as needed.

listIndexes.ok

The return value for the command. A value of 1 indicates success.

This example lists indexes for the contacts collection without specifying the cursor batch size.

1db.runCommand (
2 {
3 listIndexes: "contacts"
4 }
5)
1{
2 cursor: {
3 id: Long("0"),
4 ns: 'test.contacts',
5 firstBatch: [
6 { v: 2, key: { _id: 1 }, name: '_id_', ns: 'test.contacts' },
7 { v: 2, key: { a: 1 }, name: 'a_1', ns: 'test.contacts' }
8 ]
9 },
10 ok: 1
11}

This example lists indexes for the contacts collection, and specifies a cursor batch size of 1.

1db.runCommand (
2 {
3 listIndexes: "contacts", cursor: { batchSize: 1 }
4 }
5)
1{
2 cursor: {
3 id: Long("4809221676960028307"),
4 ns: 'test.contacts',
5 firstBatch: [ { v: 2, key: { _id: 1 }, name: '_id_', ns: 'test.contacts' } ]
6 },
7 ok: 1
8}

This example uses getMore to retrieve additional result batches from the contacts collection.

1db.runCommand(
2 {
3 getMore: Long("4809221676960028307"), collection: "contacts"
4 }
5)
1{
2 cursor: {
3 nextBatch: [ { v: 2, key: { a: 1 }, name: 'a_1', ns: 'test.contacts' } ],
4 id: Long("0"),
5 ns: 'test.contacts'
6 },
7 ok: 1
8}

Back

listDatabases