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

对集合取消分片

您可以使用unshardCollection命令对分分片的集合取消分片。 当您对集合取消分片时,该集合无法跨多个分片分区,并且分片分片键将被删除。

默认,当您对集合取消分片时, MongoDB会将集合的数据移动到分片量最少的分片。 或者,您可以分片要放置数据的分片。

您可以对以下环境中托管的部署执行此任务:

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

注意

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

  • 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.

如果您的部署启用了访问权限控制,则enableSharding角色会授予您运行unshardCollection命令的访问权限。

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

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

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

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

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

    • 确保 CPU 负载低于80 %。

1

如果要将分分片的集合中的数据放在特定分片上,则需要目标分片的名称。

要查看集群中的分分片名称列表,请使用listShards命令:

db.adminCommand( { listShards: 1 } )

shards._id 字段列出了每个分片的名称。

2

要对集合取消分片,请运行unshardCollection命令。 以下示例对sales数据库中名为us_accounts的集合取消分片:

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

取消分片操作完成后, us_accounts集合中的数据位于shard1上。 如果省略toShard字段,则数据将放置在分片量最少的分片上。

3

要确认集合未分片,请使用$shardedDataDistribution阶段并尝试在未分片的命名空间上进行匹配:

db.aggregate( [
{ $shardedDataDistribution: { } },
{ $match: { "ns": "sales.us_accounts" } }
] )

如果聚合未返回任何数据,则不对集合进行分片。