Navigation

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. You can optionally set the batch size for the first batch of results. The mongo shell provides the db.collection.getIndexes() helper.

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.

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.

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:

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.

Use getMore to retrieve additional results as needed.

listIndexes.ok

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

Examples

List Database Indexes

In this example, you list indexes for the contacts collection without specifying the cursor batch size.

1
2
3
4
5
   db.runCommand (
     {
        listIndexes: "contacts"
     }
   )

Your results will be similar to:

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

Specify Result Batch Size

In this example, you list indexes for the contacts collection, and specify a cursor batch size of 1.

1
2
3
4
5
   db.runCommand (
      {
         listIndexes: "contacts", cursor: { batchSize: 1 }
      }
   )

Your results will be similar to:

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

Retrieve Additional Results

In this example, you use getMore to retrieve additional result batches from the contacts collection.

1
2
3
4
5
   db.runCommand (
      {
         getMore: Long("4809221676960028307"), collection: "contacts"
      }
   )

Your results will be similar to:

1
2
3
4
5
6
7
8
   {
      cursor: {
       nextBatch: [ { v: 2, key: { a: 1 }, name: 'a_1', ns: 'test.contacts' } ],
       id: Long("0"),
       ns: 'test.contacts'
     },
     ok: 1
   }
←   listDatabases logRotate  →