Docs Menu
Docs Home
/
MongoDB Manual
/ / /

listIndexes

On this page

  • Definition
  • 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 the mongo Shell, this command can also be run through the db.collection.getIndexes() helper method.

Helper methods are convenient for mongo 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.

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).

New in version 4.4.

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.

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.

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, available starting in MongoDB 4.4, 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