定义
unshardCollection取消现有分片集合的分片,并将集合数据移动到单个分片上。当您对集合取消分片时,该集合无法跨多个分片分区,并且分片键将被删除。
8.0版本新增。
提示
In
mongosh, this command can also be run through thesh.unshardCollection().Helper methods are convenient for
mongoshusers, 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 Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
- MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
注意
此任务在MongoDB Atlas免费或灵活层级上不可用。
限制
不能将 unshardCollection 用于Queryable Encryption集合。
语法
db.adminCommand( { unshardCollection: "<database>.<collection>", toShard: "<shard-id>" } )
命令字段
字段 | 类型 | 必要性 | 说明 |
|---|---|---|---|
| 字符串 | 必需 | 指定要取消分片的数据库和集合。 |
| 字符串 | Optional | 指定接收分分片ID。 当MongoDB对集合进行取消分片时,会将集合数据从当前分片移动到此特定分分片。 如果未指定,集群会选择分片量最少的分片。 |
Considerations
unshardCollection只能在分分片的集群上运行。unshardCollection只能对分分片的集合进行操作。unshardCollection一次只能对一个集合进行操作。unshardCollection最短持续时间为5分钟。您必须在
unshardCollection运行后重建Atlas Search索引。writeConcernMajorityJournalDefault必须为true。在
unshardCollection完成之前,您无法进行拓扑结构更改,例如添加或删除分片或在嵌入式配置服务器和专用配置服务器之间转换。当
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
Orphanedonce 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 theActivestate.
要求
在对集合取消分片之前,请确保满足以下要求:
您的应用程序可以允许受影响的集合块进行两秒钟的写入。 在写入受阻期间,应用程序的延迟会增加。
您的数据库符合这些资源要求:
确保分片集合移动到的分片具有足够的存储空间用于集合及其索引。 目标分分片需要至少
( 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" } )
对区域分片集合取消分片
从区域中删除范围
要从区域中删除范围,运行updateZoneKeyRange 命令:
db.adminCommand( { updateZoneKeyRange: "app.inventory", min: 100, max: 500, zone: null } )
重复此步骤,直到从集合使用的区域中删除所有范围。
从区域中删除分片
要从区域中删除分片,运行removeShardFromZone 命令
db.adminCommand( { removeShardFromZone: "shard01", zone: "us" } )
重复此操作,直到从区域中删除所有集合片。