listIndexes
Definition
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 thedb.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.
Compatibility
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.
MongoDB Enterprise: The subscription-based, self-managed version of MongoDB
MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB
Syntax
The command has the following form:
db.runCommand ( { listIndexes: "<collection-name>", cursor: { batchSize: <int> }, comment: <any> } )
Command Fields
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). |
Required Access
If access control is enforced, the built-in read
role provides the
required privileges to run listIndexes
for the collections in a
database.
Behavior
Client Disconnection
Starting in MongoDB 4.2, if the client that issued listIndexes
disconnects before the operation completes, MongoDB marks listIndexes
for termination using killOp
.
Replica Set Member State Restriction
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.
Wildcard Indexes
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.
Output
listIndexes.cursor
A result set returned in the batch size specified by your cursor. Each document in the batch output contains the following fields:
FieldTypeDescriptionidintegerA 64-bit integer. If zero, there are no more batches of information. If non-zero, a cursor ID, usable in agetMore
command to get the next batch of index information.nsstringThe database and collection name in the following format:<database-name>.<collection-name>
firstBatchdocumentIndex 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.
Examples
List Database Indexes
This example lists indexes for the contacts
collection without specifying the
cursor batch size.
1 db.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 }
Specify Result Batch Size
This example lists indexes for the contacts
collection, and specifies a cursor
batch size of 1.
1 db.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 }
Retrieve Additional Results
This example uses getMore
to retrieve additional result batches from the
contacts
collection.
1 db.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 }