Definition
listIndexesReturns 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 the
mongoShell, this command can also be run through thedb.collection.getIndexes()helper method.Helper methods are convenient for
mongousers, 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.
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 |
|---|---|---|
| string | The name of the collection. |
| integer | Optional. Specifies the cursor batch size. |
| 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). New in version 4.4. |
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
Starting in MongoDB 4.4, 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.
In previous versions, the operations also run when the member
is in STARTUP2. The operations wait until the member
transitioned to RECOVERING.
Output
listIndexes.cursorA result set returned in the batch size specified by your cursor. Each document in the batch output contains the following fields:
FieldTypeDescriptionid
integer
A 64-bit integer. If zero, there are no more batches of information. If non-zero, a cursor ID, usable in a
getMorecommand 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, available starting in MongoDB 4.4, is only present if the value is true.
Use
getMoreto 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 }