对于 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: The fully managed service for MongoDB deployments in the cloud

注意

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

不能将 unshardCollection 用于Queryable Encryption集合。

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

unshardCollection

字符串

必需

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

toShard

字符串

Optional

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

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

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

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

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

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

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

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

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

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

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

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

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

  • If the collection you're unsharding uses MongoDB 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 %。

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

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

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

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

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