Database Manual / Sharding / Administration

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.从MongoDB 8.0开始,您可以使用unshardCollection命令取消对分片集合的划分。当您取消对集合进行分区时,MongoDB会将集合数据移动到单个分片上,并更新元数据以反映未分区状态。

Command Syntax命令语法

To unshard a collection, use the unshardCollection command:要取消对集合的定义,请使用unshardCollection命令:

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.如果您的部署启用了访问控制,则enableSharding角色授予您运行unshardCollection命令的权限。

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.您可以使用可选的toShard字段指定目标分片。如果不指定目标分片,MongoDB会自动选择数据量最少的分片。

Learn More了解更多