Database Manual / Sharding / Administration / Unsharded Collections

Unshard a Collection解开集合

You can unshard a sharded collection with the unshardCollection command. 您可以使用unshardCollection命令取消对分片集合的设置。When you unshard a collection, the collection cannot be partitioned across multiple shards and the shard key is removed.当您取消对集合的分区时,该集合不能跨多个分片进行分区,并且分片键将被删除。

By default, when you unshard a collection, MongoDB moves the collection's data to the shard with the least amount of data. Alternatively, you can specify which shard to place the data on.默认情况下,当您取消对集合进行分区时,MongoDB会将集合的数据移动到数据量最少的分片。或者,您可以指定将数据放置在哪个分片上。

About this Task关于此任务

Compatibility兼容性

You can perform this task on deployments hosted in the following environments:您可以在以下环境中托管的部署上执行此任务:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Note

This task is not available on the MongoDB Atlas Free or Flex Tiers.此任务在MongoDB Atlas Free或Flex层上不可用。

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Restrictions限制

  • unshardCollection can only be run on sharded clusters.只能在分片集群上运行。
  • unshardCollection can only operate on sharded collections.只能对分片集合进行操作。
  • unshardCollection can only operate on a single collection at a time.一次只能对单个集合进行操作。
  • unshardCollection has a 5 minute minimum duration.最小持续时间为5分钟。
  • You must rebuild MongoDB Search indexes after unshardCollection runs.您必须在unshardCollection运行后重建MongoDB搜索索引。
  • You cannot make topology changes, such as adding or removing shards or transitioning between embedded and dedicated config servers, until unshardCollection completes.unshardCollection完成之前,您无法进行拓扑更改,例如添加或删除分片或在嵌入式和专用配置服务器之间转换。
  • You cannot run the following operations on the collection that is being unsharded while unshardCollection is in progress:unshardCollection正在进行时,您无法对未被记录的集合运行以下操作:

  • You cannot run the following operations on the cluster while unshardCollection is in progress:unshardCollection正在进行时,您无法在群集上运行以下操作:

  • Index builds that occur while unshardCollection is in progress might silently fail.unshardCollection进行过程中发生的索引构建可能会自动失败。

    • Do not create indexes while unshardCollection is in progress.unshardCollection正在进行时,请勿创建索引。
    • Do not call unshardCollection if there are ongoing index builds.如果有正在进行的索引构建,请不要调用unshardCollection
  • To avoid error, MongoDB automatically drops the zones in your collection when you run unshardCollection.为了避免错误,当您运行unshardCollection时,MongoDB会自动删除集合中的区域。

Access Control访问控制

If your deployment has access control enabled, the enableSharding role grants you access to run the unshardCollection command.如果部署启用了访问控制,则enableSharding角色授予您运行unshardCollection命令的权限。

Before you Begin开始之前

Before you unshard your collection, ensure that you meet the following requirements:在取消集合的分级之前,请确保满足以下要求:

  • Your application can tolerate a period of two seconds where the affected collection blocks writes. During the time period where writes are blocked, your application experiences an increase in latency.应用程序可以容忍受影响的集合块写入的两秒钟时间。在写入被阻止的时间段内,应用程序的延迟会增加。
  • Your database meets these resource requirements:数据库满足以下资源要求:

    • Ensure the shard you are moving the collection to has enough storage space for the collection and its indexes. The destination shard requires at least ( Collection storage size + Index Size ) * 2 bytes available.确保将集合移动到的分片有足够的存储空间用于集合及其索引。目标分片至少需要(集合存储大小+索引大小)*2个可用字节。
    • Ensure that your I/O capacity is below 50%.确保I/O容量低于50%。
    • Ensure that your CPU load is below 80%.确保CPU负载低于80%。

Steps步骤

1

(Optional) List shard names(可选)列出分片名称

If you want to put the data from your sharded collection on a specific shard, you need the target shard's name.如果你想将分片集合中的数据放在特定的分片上,你需要目标分片的名称。

To see the list of shard names in your cluster, use the listShards command:要查看集群中的分片名称列表,请使用listShards命令:

db.adminCommand( { listShards: 1 } )

The shards._id field lists the name of each shard.shards._id字段列出了每个分片的名称。

2

Unshard the collection解开集合

To unshard a collection, run the unshardCollection command. The following example unshards a collection called us_accounts in the sales database:要取消对集合的定义,请运行unshardCollection命令。以下示例取消了销售数据库中名为us_accounts的集合的屏蔽:

db.adminCommand( {
unshardCollection: "sales.us_accounts",
toShard: "shard1"
} )

After the unshard operation completes, the data in the us_accounts collection is on shard1. If you omit the toShard field, the data is placed on the shard with the least amount of data.非共享操作完成后,us_accounts集合中的数据将位于shard1上。如果省略toShard字段,则数据将放置在数据量最少的分片上。

3

Confirm that the collection is unsharded确认集合未被记录

To confirm that the collection is unsharded, use the $shardedDataDistribution stage and try to match on the unsharded namespace:要确认集合未被记录,请使用$shardedDataDistribution阶段,并尝试在未被记录的命名空间上进行匹配:

db.aggregate( [
{ $shardedDataDistribution: { } },
{ $match: { "ns": "sales.us_accounts" } }
] )

If the aggregation doesn't return any data, the collection is unsharded.如果聚合未返回任何数据,则集合未被记录。

Learn More了解更多