对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

unshardCollection(数据库命令)

unshardCollection

取消现有分片集合的分片,并将集合数据移动到单个分片上。当您对集合取消分片时,该集合无法跨多个分片分区,并且分片键将被删除。

8.0版本新增。

提示

In mongosh, this command can also be run through the 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.

此命令必须在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.

注意

对集合取消分片是一项写入密集型操作,可能会导致oplog增长率加快。 为帮助缓解此问题,请考虑对配置进行以下更改:

  • 要防止oplog无限制增长,请设立固定的oplog大小。

  • 要降低从节点过时的可能性,请增加oplog的大小。

有关更多详细信息,请参阅副本集oplog 。

此命令可用于以下环境中托管的部署:

  • MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务

注意

此任务在MongoDB Atlas免费或灵活层级上不可用。

不能将 unshardCollection 用于Queryable Encryption集合。

db.adminCommand( {
unshardCollection: "<database>.<collection>",
toShard: "<shard-id>"
} )
字段
类型
必要性
说明

unshardCollection

字符串

必需

指定要取消分片的数据库和集合。

toShard

字符串

Optional

指定接收分分片ID。 当MongoDB对集合进行取消分片时,会将集合数据从当前分片移动到此特定分分片。

如果未指定,集群会选择分片量最少的分片。

  • unshardCollection 只能在分分片的集群上运行。

  • unshardCollection 只能对分分片的集合进行操作。

  • unshardCollection 一次只能对一个集合进行操作。

  • unshardCollection 最短持续时间为5分钟。

  • 您必须在unshardCollection运行后重建Atlas Search索引。

  • unshardCollection完成之前,您无法进行拓扑结构更改,例如添加或删除分片或在嵌入式配置服务器和专用配置服务器之间转换。

  • unshardCollection正在进行时,您无法对集群运行以下操作:

  • unshardCollection正在进行时进行的索引构建可能会静默失败。

    • 正在进行unshardCollection时,请勿创建索引。

    • 如果正在进行索引构建,请勿调用unshardCollection

  • 为了避免错误,当您运行unshardCollection 时, MongoDB会自动删除集合中的区域。

  • If the collection you're unsharding uses Atlas Search, the search index becomes unavailable when the unsharding operation completes. You need to manually rebuild the search index once the unsharding operation completes.

  • If the collection you're unsharding is archived in Atlas Online Archives, the online archive files are marked as Orphaned once the unsharding operation completes. You can create another online archive for the same database, collection, and fields as the orphaned archive as long as there is no other archive for that same combination in the Active state.

在对集合取消分片之前,请确保满足以下要求:

  • 您的应用程序可以允许受影响的集合块进行两秒钟的写入。 在写入受阻期间,应用程序的延迟会增加。

  • 您的数据库符合这些资源要求:

    • 确保分片集合移动到的分片具有足够的存储空间用于集合及其索引。 目标分分片需要至少( Collection storage size + Index Size ) * 2字节可用。

    • 确保 I/O容量低于50 %。

    • 确保 CPU 负载低于80 %。

要对使用区域分片的集合取消分片,必须先停止负载均衡器,然后从区域中删除范围和分片。

For an example, see Unshard a Zone Sharded Collection.

以下示例对sales.eu_accounts集合取消分片:

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

以下示例对sales.us_accounts集合进行取消分片,并将集合数据放在shard1上:

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

以下示例对使用区域的集合取消分片:

1

要停止负载均衡器,运行balancerStop 命令:

db.adminCommand( { balancerStop: 1 } )
2

要确定与区域关联的范围,运行sh.status() 方法并记下每个分片的集合的chunks 字段中的范围:

sh.status()
3

要从区域中删除范围,运行updateZoneKeyRange 命令:

db.adminCommand( {
updateZoneKeyRange: "app.inventory",
min: 100,
max: 500,
zone: null
} )

重复此步骤,直到从集合使用的区域中删除所有范围。

4

要从区域中删除分片,运行removeShardFromZone 命令

db.adminCommand( {
removeShardFromZone: "shard01",
zone: "us"
} )

重复此操作,直到从区域中删除所有集合片。

5

要重新启动负载均衡器,运行balancerStart 命令:

db.adminCommand( { balancerStart: 1 } )
6

要对集合取消分片,运行unshardCollection 命令:

db.adminCommand( { unshardCollection: "app.inventory" } )