Docs Menu
Docs Home
/
MongoDB Manual
/ /

Drop a Hashed Shard Key Index

On this page

  • About this Task
  • Steps
  • Learn More

Starting in MongoDB 5.0.22, you can drop the index for a hashed shard key.

This can speed up data insertion for collections sharded with a hashed shard key. It can also speed up data ingestion when using mongosync.

Dropping an unnecessary index can speed up CRUD operations. Each CRUD operation has to update all the indexes related to a document. Removing one index can increase the speed of all CRUD operations.

When you drop a hashed shard key index, the server disables balancing for that collection and excludes the collection from future balancing rounds. In order to include the collection in balancing rounds once again, you must recreate the shard key index.

1

Run the following command to stop the balancer:

sh.stopBalancer()

You can only run sh.stopBalancer() on mongos. sh.stopBalancer() produces an error if run on mongod.

2

Run the following command to confirm no orphaned documents remain:

db.runCommand(
{
cleanupOrphaned: "<database>.<collection>"
}
)

cleanupOrphaned returns either 1 or 0.

A value of 1 indicates that either:

  • No orphaned documents remain in the cleanupOrphaned namespace on the shard

  • The collection referenced in the cleanupOrphaned namespace is not sharded

A value of 0 indicates that an error has occurred.

Repeat this step until the command returns 1.

3

Run the following command to drop the index:

db.collection.dropIndex("<index name>")
4

Run the following command to restart the balancer on the cluster:

sh.startBalancer()
← Convert a Replica Set to a Sharded Cluster