Unsharded Collections
Starting in MongoDB 8.0, you can unshard a sharded collection by using the
unshardCollection
command. When you unshard a collection,
MongoDB moves the collection data onto a single shard and updates the metadata
to reflect the unsharded state.
Command Syntax
To unshard a collection, use the unshardCollection
command:
db.adminCommand({ unshardCollection : "<database>.<collection>", toShard : "<recipient shard ID>" })
Use Cases
A user can unshard a collection if:
You can store the collection entirely on a single shard.
The collection requires resource isolation, and access patterns are better supported if the collection lives on a single shard. To meet the same requirements on a sharded collection, see Zone Sharding.
The collection was previously sharded but no longer needs to be sharded.
Get Started
Access Control
If your deployment has access control enabled,
the enableSharding
role grants you access to run the
unshardCollection
command.
Details
An unsharded collection's data only lives on one shard and the shard key is removed. Collections that you manually unshard behave the same as newly created collections that were never sharded.
You can specify the destination shard with the optional toShard
field. If
you don't specify a destination shard, MongoDB automatically selects the shard
with the least amount of data.