Manage Indexes
On this page
This page shows how to manage existing indexes. For instructions on creating indexes, refer to the specific index type pages.
View Existing Indexes
- Mongo Shell
- Compass
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.
For details on the information displayed in this tab, refer to the Compass documentation .
- Mongo Shell
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:
- Mongo Shell
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:
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
- 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 the tax-id
field in the accounts
collection:
The operation returns a document with the status of the operation:
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:
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:
- Navigate to the collection on which the target index exists.
- Click the Indexes tab.
- Click the trash can icon in the Drop 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 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.
- Compass