Manage Indexes

This page shows how to manage existing indexes. For instructions on creating indexes, refer to the specific index type pages.

View Existing Indexes

The following sections provide methods for viewing existing indexes on a collection or an entire database.

To view a list of all indexes on a collection in MongoDB Compass, click on the target collection in the left-hand pane and select the Indexes tab.

View indexes on a collection in Compass

For details on the information displayed in this tab, refer to the Compass documentation .

List all Indexes on a Collection

To return a list of all indexes on a collection, use the db.collection.getIndexes() method or a similar method for your driver .

For example, to view all indexes on the people collection, run the following command:

db.people.getIndexes()

List all Indexes on a Database

To list all indexes on all collections in a database, you can use the following operation in the mongo shell:

db.getCollectionNames().forEach(function(collection) {
   indexes = db[collection].getIndexes();
   print("Indexes for " + collection + ":");
   printjson(indexes);
});

Changed in version 3.0: MongoDB 3.0 deprecates direct access to the system.indexes collection, which had previously been used to list all indexes in a database.

For MongoDB 3.0 deployments using the WiredTiger storage engine, if you run db.getCollectionNames() and db.collection.getIndexes() from a version of the mongo shell before 3.0 or a version of the driver prior to 3.0 compatible version, db.getCollectionNames() and db.collection.getIndexes() will return no data, even if there are existing collections and indexes. For more information, see WiredTiger and Driver Version Compatibility.

Remove Indexes

MongoDB provides two methods for removing indexes from a collection:

Remove Specific Index

To remove an index, use the db.collection.dropIndex() method.

For example, the following operation removes an ascending index on the tax-id field in the accounts collection:

db.accounts.dropIndex( { "tax-id": 1 } )

The operation returns a document with the status of the operation:

{ "nIndexesWas" : 3, "ok" : 1 }

Where the value of nIndexesWas reflects the number of indexes before removing this index.

For text indexes, pass the index name to the db.collection.dropIndex() method. See Use the Index Name to Drop a text Index for details.

Remove All Indexes

You can also use the db.collection.dropIndexes() to remove all indexes except for the _id index from a collection.

For example, the following command removes all indexes from the accounts collection:

db.accounts.dropIndexes()

These shell helpers provide wrappers around the dropIndexes database command. Your client library may have a different or additional interface for these operations.

To remove an index from a collection in MongoDB Compass:

  1. Navigate to the collection on which the target index exists.
  2. Click the Indexes tab.
  3. Click the trash can icon in the Drop column for the index you wish to delete.
Delete an index in Compass

Modify an Index

To modify an existing index, you need to drop and recreate the index. The exception to this rule is TTL indexes, which can be modified via the collMod command in conjunction with the index collection flag.

To modify an existing index in MongoDB Compass, you need to drop and recreate the index.