Manage Indexes

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

View Existing Indexes

  • Mongo Shell
  • Compass

The following sections provide methods for viewing existing indexeson 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 andselect the Indexes tab.

View indexes on a collection in Compass

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

  • Mongo Shell

List all Indexes on a Collection

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

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

  1. db.people.getIndexes()
  • Mongo Shell

List All Indexes for a Database

To list all the collection indexes in a database, you can use thefollowing operation in the mongo shell:

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

Starting in version 3.0, MongoDB deprecates direct access tothe system.indexes collection, which had previously beenused to list all indexes in a database.

List Specific Type of Indexes

To list all indexes of a certain type (e.g. hashed, text) for allcollections in all database, you can use the followingoperation in the mongo shell:

  1. // The following finds all hashed indexes
  2.  
  3. db.adminCommand("listDatabases").databases.forEach(function(d){
  4. let mdb = db.getSiblingDB(d.name);
  5. mdb.getCollectionInfos({ type: "collection" }).forEach(function(c){
  6. let currentCollection = mdb.getCollection(c.name);
  7. currentCollection.getIndexes().forEach(function(idx){
  8. let idxValues = Object.values(Object.assign({}, idx.key));
  9.  
  10. if (idxValues.includes("hashed")) {
  11. print("Hashed index: " + idx.name + " on " + idx.ns);
  12. printjson(idx);
  13. };
  14. });
  15. });
  16. });

Remove Indexes

  • Mongo Shell
  • Compass

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 thetax-id field in the accounts collection:

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

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

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

Where the value of nIndexesWas reflects the number of indexesbefore removing this index.

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

Note

Starting in MongoDB 4.2,db.collection.dropIndexes() can accept an arrayof index names.

Remove All Indexes

You can also use the db.collection.dropIndexes() to removeall indexes except for the _id index from acollection.

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

  1. db.accounts.dropIndexes()

These shell helpers provide wrappers around thedropIndexesdatabase command. Your clientlibrary may have a different or additionalinterface for these operations.

To remove an index from a collection in MongoDB Compass:

  • Navigate to the collection on which the targetindex exists.
  • Click the Indexes tab.
  • Click the trash can icon in theDrop column for the index you wish to delete.Delete an index in Compass

Modify an Index

  • Mongo Shell
  • Compass

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

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

  • Compass

See also