- Indexes >
- Manage Indexes
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 for a Database
To list all the collection indexes in a database, you can use the
following operation in the mongo
shell:
Starting in version 3.0, MongoDB deprecates direct access to
the system.indexes
collection, which had previously been
used 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 all
collections in all database, you can use the following
operation in the mongo
shell:
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.
Note
Starting in MongoDB 4.2,
db.collection.dropIndexes()
can accept an array
of index names.
Starting in MongoDB 4.4,
db.collection.dropIndexes()
can stop
in-progress index builds. See
Aborts In-Progress Index Builds for more information.
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
Find Inconsistent Indexes across Shards¶
A sharded collection has an inconsistent index if the collection does not have the exact same indexes (including the index options) on each shard that contains chunks for the collection. Although inconsistent indexes should not occur during normal operations, inconsistent indexes can occur , such as:
- When a user is creating an index with a
unique
key constraint and one shard contains a chunk with duplicate documents. In such cases, the create index operation may succeed on the shards without duplicates but not on the shard with duplicates. - When a user is creating an index across the shards in a rolling manner (i.e. manually building the index one by one across the shards) but either fails to build the index for an associated shard or incorrectly builds an index with different specification.
Starting in MongoDB 4.4 (and 4.2.6), the config server primary, by default, checks for
index inconsistencies across the shards for sharded collections, and
the command serverStatus
, when run on the config server
primary, returns the field shardedIndexConsistency
field to report on the number of sharded collections with index
inconsistencies.
If shardedIndexConsistency
reports any index
inconsistencies, you can run the following pipeline for your
sharded collections until you find the inconsistencies.
Note
The following pipeline is for MongoDB 4.2.4 and above.
Define the following aggregation pipeline:
Run the aggregation pipeline for the sharded collection to test. For example, to test if the sharded collection
test.reviews
has inconsistent indexes across its associated shards:If the collection has inconsistent indexes, the aggregation for that collection returns details regarding the inconsistent indexes:
The returned documents indicate two inconsistencies for the sharded collection
test.reviews
:- An index named
page_1_score_1
is missing from the collection onshardB
. - An index named
reviewDt_1
has inconsistent properties across the collection’s shards, specifically, theexpireAfterSeconds
properties differ.
- An index named
- To resolve the inconsistency where an index is missing from the collection on a particular shard(s),
You can either:
Perform a rolling index build for the collection on the affected shard(s).
-OR-
Issue an index build
db.collection.createIndex()
from amongos
instance. The operation only builds the collection’s index on the shard(s) missing the index.
- To resolve where the index properties differ across the shards,
Drop the incorrect index from the collection on the affected shard(s) and rebuild the index. To rebuild the index, you can either:
Perform a rolling index build for the collection on the affected shard.
-OR-
Issue an index build
db.collection.createIndex()
from amongos
instance. The operation only builds the collection’s index on the shard(s) missing the index.
Alternatively, if the inconsistency is the
expireAfterSeconds
property, you can run thecollMod
command to update the number of seconds instead of dropping and rebuilding the index.