Manage Indexes

The following procedures provides some common procedures for managing existing indexes. For instructions on creating indexes, refer to the specific index type pages.

View Existing Indexes

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:


List all Indexes for 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 + ":");

MongoDB 3.0 deprecates direct access to the system.indexes collection.

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

To remove an index from a collection, you can use the db.collection.dropIndex() method.

Remove a 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.

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

Modify an Index

To modify an existing index, you need to drop and recreate the index with the exception of m TTL indexes.

To build indexes for a replica set, see Build Indexes on Replica Sets.