Database Manual / Reference / Database Commands / Sharding

unshardCollection (database command数据库命令)

Definition定义

unshardCollection

Unshards an existing sharded collection and moves the collection data onto a single shard. When you unshard a collection, the collection cannot be partitioned across multiple shards and the shard key is removed.解开现有的分片集合,并将集合数据移动到单个分片上。当您取消对集合的分区时,该集合不能跨多个分片进行分区,并且分片键将被删除。

New in version 8.0.在版本8.0中新增。

Tip

In mongosh, this command can also be run through the sh.unshardCollection().mongosh中,此命令也可以通过sh.unshardCollection()运行。

Helper methods are convenient for mongosh users, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.助手方法对mongosh用户来说很方便,但它们可能不会返回与数据库命令相同级别的信息。如果不需要便利性或需要额外的返回字段,请使用database命令。

This command must run on the admin database.此命令必须在admin数据库上运行。

If the collection has a zone configuration applied, you must first remove the range associations and shards from the zone before you unshard the collection. 如果集合应用了区域配置,则必须首先从区域中删除范围关联和分片,然后才能取消对集合的定义。For more information, see Unshard Zones.有关详细信息,请参阅未分片区域

Note

Unsharding a collection is a write-intensive operation that can result in an increased oplog growth rate. To help mitigate this, consider the following configuration changes:对集合进行解包是一项写密集型操作,可能会导致oplog增长率增加。为了帮助缓解这种情况,请考虑以下配置更改:

  • To prevent unbounded oplog growth, set a fixed oplog size.为了防止oplog无限增长,请设置一个固定的oplog大小。
  • To reduce the chance of secondaries becoming stale, increase the oplog size.为了减少中学过时的机会,请增加oplog大小。

For more details, see the Replica Set Oplog.有关更多详细信息,请参阅副本集操作日志

Compatibility兼容性

This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:

  • 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的源代码可用、免费使用和自我管理版本
  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Important

This command cannot be run on shared or serverless instances. For more information, see Unsupported Commands.此命令无法在共享或无服务器实例上运行。有关详细信息,请参阅不支持的命令

Restrictions限制

You cannot use unshardCollection for Queryable Encryption collections.您不能将unshardCollection用于可查询加密集合。

Syntax语法

db.adminCommand( {
unshardCollection: "<database>.<collection>",
toShard: "<shard-id>"
} )

Command Fields命令字段

Field字段Type类型Necessity必要性Description描述
unshardCollectionstring字符串Required必需Specifies the database and collection to unshard.指定要取消定义的数据库和集合。
toShardstring字符串Optional可选

Specifies the recipient shard ID. As MongoDB unshards the collection, it moves collection data from their current shards to this specific shard.指定收件人分片ID。当MongoDB取消对集合进行分片时,它会将集合数据从其当前分片移动到此特定分片。

If unspecified, the cluster selects the shard with the least amount of data.如果未指定,集群将选择数据量最少的分片。

Considerations注意事项

  • 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会自动删除集合中的区域。

Requirements需求

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%。

Behavior行为

Unshard Zones未分片区域

To unshard a collection that uses zone sharding, you must first stop the balancer, then remove the range and shard from the zone.要取消使用区域分片的集合的分片,您必须首先停止平衡器,然后从区域中删除范围和分片。

Examples示例

Unshard a Collection未分片集合

The following example unshards the sales.eu_accounts collection:以下示例取消了sales.eu_accounts集合的屏蔽:

db.adminCommand( {
unshardCollection: "sales.eu_accounts"
} )

Unshard to a Specific Shard对特定分片取消分片

The following example unshards the sales.us_accounts collections and places the collection data on shard1:以下示例取消对sales.us_accounts集合的屏蔽,并将集合数据放置在shard1上:

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

Learn More了解更多