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

sh.unshardCollection(mongosh方法)

sh.unshardCollection( namespace, shardID )

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

8.0版本新增。

重要

mongosh 方法

This page documents a mongosh method. This is not the documentation for database commands or language-specific drivers, such as Node.js.

有关数据库命令,请参阅 unshardCollection 命令。

如需了解 MongoDB API 驱动程序,请参阅特定语言的 MongoDB 驱动程序文档。

sh.unshardCollection方法要求您指定分片集合数据的分片。 使用unshardCollection命令时,如果未指定目标分分片,集群会自动选择数据分片的分片。

If the collection uses zone sharding, you must first remove the range associations and shard from the zone before you unshard the collection. For more information, see Unshard Zones.

注意

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

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

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

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

sh.unshardCollection 通过以下语法实现:

sh.unshardCollection( namespace, shardID )
Parameter
类型
说明

namespace

字符串

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

shardID

字符串

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

此方法可用于以下环境中托管的部署:

  • MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
  • sh.unshardCollection() 只能在分分片的集群上运行。

  • sh.unshardCollection() 只能对分分片的集合进行操作。

  • sh.unshardCollection() 一次只能对一个集合进行操作。

  • sh.unshardCollection() 最短持续时间为5分钟。

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

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

  • sh.unshardCollection()正在进行时,您无法对正在取消分片的集合运行以下操作:

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

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

    • 正在进行sh.unshardCollection()时,请勿创建索引。

    • 如果正在进行索引构建,请勿调用sh.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.

此示例将app数据库上名为inventory的集合取消分片到shard02分分片。

sh.unshardCollection( "app.inventory", "shard02" )

要获取可用分分片ID 的列表,请运行sh.status() 。 有关详细信息,请参阅sh.status() 输出。

此示例对使用区域的集合进行取消分片:

1

要停止负载均衡器,运行sh.stopBalancer() 方法:

sh.stopBalancer()
2

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

sh.status()
3

要从区域中删除范围,请使用sh.removeRangeFromZone() 方法:

sh.removeRangeFromZone( {
"app.inventory",
{ size: 100 },
{ size: 500 }
} )

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

4

要从区域中删除分片,运行sh.removeShardFromZone() 方法:

sh.removeShardFromZone( "shard01", "mid" )

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

5

要重新启动负载均衡器,运行sh.startBalancer() 方法:

sh.startBalancer()
6

要对集合取消分片,运行sh.unshardCollection 方法:

sh.unshardCollection( "app.inventory", "shard01" )