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限制
unshardCollectioncan only be run on sharded clusters.只能在分片集群上运行。unshardCollectioncan only operate on sharded collections.只能对分片集合进行操作。unshardCollectioncan only operate on a single collection at a time.一次只能对单个集合进行操作。unshardCollectionhas a 5 minute minimum duration.最小持续时间为5分钟。You must rebuild MongoDB Search indexes after您必须在unshardCollectionruns.unshardCollection运行后重建MongoDB搜索索引。You cannot make topology changes, such as adding or removing shards or transitioning between embedded and dedicated config servers, until在unshardCollectioncompletes.unshardCollection完成之前,您无法进行拓扑更改,例如添加或删除分片或在嵌入式和专用配置服务器之间转换。You cannot run the following operations on the collection that is being unsharded while当unshardCollectionis in progress:unshardCollection正在进行时,您无法对未被记录的集合运行以下操作:You cannot run the following operations on the cluster while当unshardCollectionis in progress:unshardCollection正在进行时,您无法在群集上运行以下操作:Index builds that occur while在unshardCollectionis in progress might silently fail.unshardCollection进行过程中发生的索引构建可能会自动失败。Do not create indexes whileunshardCollectionis in progress.unshardCollection正在进行时,请勿创建索引。Do not call如果有正在进行的索引构建,请不要调用unshardCollectionif 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确保将集合移动到的分片有足够的存储空间用于集合及其索引。目标分片至少需要(集合存储大小+索引大小)*2个可用字节。( Collection storage size + Index Size ) * 2bytes available.Ensure that your I/O capacity is below 50%.确保I/O容量低于50%。Ensure that your CPU load is below 80%.确保CPU负载低于80%。
Steps步骤
(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字段列出了每个分片的名称。
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字段,则数据将放置在数据量最少的分片上。
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.如果聚合未返回任何数据,则集合未被记录。