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.
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:
- 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:
- db.getCollectionNames().forEach(function(collection) {
- indexes = db[collection].getIndexes();
- print("Indexes for " + collection + ":");
- printjson(indexes);
- });
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:
- // The following finds all hashed indexes
- db.adminCommand("listDatabases").databases.forEach(function(d){
- let mdb = db.getSiblingDB(d.name);
- mdb.getCollectionInfos({ type: "collection" }).forEach(function(c){
- let currentCollection = mdb.getCollection(c.name);
- currentCollection.getIndexes().forEach(function(idx){
- let idxValues = Object.values(Object.assign({}, idx.key));
- if (idxValues.includes("hashed")) {
- print("Hashed index: " + idx.name + " on " + idx.ns);
- printjson(idx);
- };
- });
- });
- });
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:
- 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 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:
- db.accounts.dropIndexes()
These shell helpers provide wrappers around thedropIndexes
database 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.
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