unshardCollection
定义
unshardCollection
取消对现有分片集合的分片,并将集合数据移动到单个分片。当取消分片集合时,集合不能跨多个分片分区,并且移除分片键。
新增在版本8.0.
提示
在
mongosh
中,此命令也可以通过sh.unshardCollection()
运行。辅助方法对于
mongosh
用户来说很方便,但它们可能不会返回与数据库命令相同级别的信息。在不需要方便或需要额外的返回字段的情况下,请使用数据库命令。此命令必须在
admin
数据库上运行。
兼容性
此命令可在以下环境中部署的以下环境中使用
MongoDB Enterprise:MongoDB的基于订阅的、自管理的版本
MongoDB Community:MongoDB的开源、免费使用和自管理版本
MongoDB Atlas:云中MongoDB部署的完全托管服务
重要
此命令不能在共享或无服务器实例上运行。有关更多信息,请参阅不支持的命令。
限制
您不能为以下内容使用unshardCollection
语法
db.adminCommand( { unshardCollection: "<database>.<collection>", toShard: "<shard-id>" } )
命令字段
字段 | 类型 | 必要性 | 描述 |
---|---|---|---|
unshardCollection | string | 必填 | 指定要取消分片的数据库和集合。 |
toShard | string | 可选 | 指定接收者分片ID。在MongoDB取消分片集合时,它会将集合数据从当前分片移动到该特定分片。 如果未指定,集群将选择数据量最少的分片。 |
注意事项
unshardCollection
只能在分片集群上运行。unshardCollection
只能操作分片集合。unshardCollection
每次只能操作一个集合。unshardCollection
至少需要5分钟。在运行
unshardCollection
后,必须重建 Atlas Search 索引。在
unshardCollection
完成之前,不能进行拓扑更改,例如添加或删除分片或在不同配置服务器之间切换。在
unshardCollection
进行期间,不能在正在取消分片的集合上运行以下操作在
unshardCollection
进行期间,不能在集群上运行以下操作在
unshardCollection
进行期间发生的索引构建可能会静默失败。在
unshardCollection
进行期间,不要创建索引。如果有正在进行的索引构建,请不要调用
unshardCollection
。
需求
在您取消分片您的集合之前,请确保您满足以下要求
您的应用程序可以容忍在受影响的集合阻止写入期间,出现两秒钟的延迟。在此时间段内,您的应用程序将经历延迟增加。
您的数据库满足以下资源要求
确保您要移动集合到的分片有足够的存储空间来存储该集合及其索引。目标分片至少需要
( 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" } )