> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# listIndexes (database command)

## Definition

Returns information about the indexes on the specified collection, including [hidden indexes](https://www.mongodb.com/docs/manual/core/index-hidden.md#std-label-index-type-hidden) and indexes that are currently being built. 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.

**Tip:**

In [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#std-program-mongosh), this command can also be run through the [`db.collection.getIndexes()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.getIndexes.md#mongodb-method-db.collection.getIndexes) helper method.

Helper methods are convenient for [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#std-program-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](https://www.mongodb.com/docs/atlas): The fully managed service for MongoDB deployments in the cloud

**Note:**

This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see [Unsupported Commands.](https://www.mongodb.com/docs/atlas/unsupported-commands/)

- [MongoDB Enterprise](https://www.mongodb.com/docs/manual/administration/install-enterprise.md#std-label-install-mdb-enterprise): The subscription-based, self-managed version of MongoDB

- [MongoDB Community](https://www.mongodb.com/docs/manual/administration/install-community.md#std-label-install-mdb-community-edition): The source-available, free-to-use, and self-managed version of MongoDB

## Syntax

The command has the following syntax:

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

## Command Fields

The command 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: [mongod log messages](https://www.mongodb.com/docs/manual/reference/log-messages.md#std-label-log-messages-ref), in the `attr.command.cursor.comment` field.; [Database profiler](https://www.mongodb.com/docs/manual/reference/database-profiler.md#std-label-profiler) output, in the [`command.comment`](https://www.mongodb.com/docs/manual/reference/database-profiler.md#mongodb-data-system.profile.command) field.; [`currentOp`](https://www.mongodb.com/docs/manual/reference/command/currentOp.md#mongodb-dbcommand-dbcmd.currentOp) output, in the [`command.comment`](https://www.mongodb.com/docs/manual/reference/command/currentOp.md#mongodb-data-currentOp.command) field. A comment can be any valid [BSON type](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-bson-types) (string, integer, object, array, etc). Any comment set on a `listIndexes` command is inherited by any subsequent [`getMore`](https://www.mongodb.com/docs/manual/reference/command/getMore.md#mongodb-dbcommand-dbcmd.getMore) commands run on the `listIndexes` cursor. |

## Required Access

If access control is enforced, the built-in [`read`](https://www.mongodb.com/docs/manual/reference/built-in-roles.md#mongodb-authrole-read) role provides the required privileges to run [`listIndexes`](https://www.mongodb.com/docs/manual/reference/command/listIndexes.md#mongodb-dbcommand-dbcmd.listIndexes) for the collections in a database.

## Behavior

### MongoDB Search Indexes

`listIndexes` does not return information on [MongoDB Search indexes](https://www.mongodb.com/docs/atlas/atlas-search/atlas-search-overview/#fts-indexes). Instead, use [`$listSearchIndexes`.](https://www.mongodb.com/docs/manual/reference/operator/aggregation/listSearchIndexes.md#mongodb-pipeline-pipe.-listSearchIndexes)

### Client Disconnection

If the client that issued [`listIndexes`](https://www.mongodb.com/docs/manual/reference/command/listIndexes.md#mongodb-dbcommand-dbcmd.listIndexes) disconnects before the operation completes, MongoDB marks [`listIndexes`](https://www.mongodb.com/docs/manual/reference/command/listIndexes.md#mongodb-dbcommand-dbcmd.listIndexes) for termination using [`killOp`.](https://www.mongodb.com/docs/manual/reference/command/killOp.md#mongodb-dbcommand-dbcmd.killOp)

### Replica Set Member State Restriction

To run on a replica set member, [`listIndexes`](https://www.mongodb.com/docs/manual/reference/command/listIndexes.md#mongodb-dbcommand-dbcmd.listIndexes) operations require the member to be in [`PRIMARY`](https://www.mongodb.com/docs/manual/reference/replica-states.md#mongodb-replstate-replstate.PRIMARY) or [`SECONDARY`](https://www.mongodb.com/docs/manual/reference/replica-states.md#mongodb-replstate-replstate.SECONDARY) state. If the member is in another state, such as [`STARTUP2`](https://www.mongodb.com/docs/manual/reference/replica-states.md#mongodb-replstate-replstate.STARTUP2), the operation errors.

### Wildcard Indexes

Starting in MongoDB 6.3, 6.0.5, and 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`](https://www.mongodb.com/docs/manual/reference/command/listIndexes.md#mongodb-dbcommand-dbcmd.listIndexes) and [`db.collection.getIndexes()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.getIndexes.md#mongodb-method-db.collection.getIndexes) commands.

## Output

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`](https://www.mongodb.com/docs/manual/reference/command/getMore.md#mongodb-dbcommand-dbcmd.getMore) to retrieve additional results as needed. |

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

## Examples

### List Database Indexes

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

```json
db.runCommand (
  {
     listIndexes: "contacts"
  }
)
```

**Output:**

```text
{
   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

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

```json
db.runCommand (
   {
      listIndexes: "contacts", cursor: { batchSize: 1 }
   }
)
```

**Output:**

```text
{
   cursor: {
     id: Long("4809221676960028307"),
     ns: 'test.contacts',
    firstBatch: [ { v: 2, key: { _id: 1 }, name: '_id_', ns: 'test.contacts' } ]
  },
  ok: 1
}
```

### Retrieve Additional Results

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

```json
db.runCommand(
   {
      getMore: Long("4809221676960028307"), collection: "contacts"
   }
)
```

**Output:**

```text
{
   cursor: {
    nextBatch: [ { v: 2, key: { a: 1 }, name: 'a_1', ns: 'test.contacts' } ],
    id: Long("0"),
    ns: 'test.contacts'
  },
  ok: 1
}
```
