定义
sh.unshardCollection( namespace, shardID )取消现有分片集合的分片,并将集合数据移动到单个分片上。当您对集合取消分片时,该集合无法跨多个分片分区,并且分片键将被删除。
8.0版本新增。
重要
mongosh 方法
This page documents a
mongoshmethod. 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 | 类型 | 说明 |
|---|---|---|
| 字符串 | 指定要取消分片的数据库和集合。 |
| 字符串 | 指定接收分分片ID。 当MongoDB对集合进行取消分片时,会将集合数据从当前分片移动到此特定分分片。 |
兼容性
此方法可用于以下环境中托管的部署:
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
- MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
Considerations
sh.unshardCollection()只能在分分片的集群上运行。sh.unshardCollection()只能对分分片的集合进行操作。sh.unshardCollection()一次只能对一个集合进行操作。sh.unshardCollection()最短持续时间为5分钟。您必须在
sh.unshardCollection()运行后重建Atlas Search索引。writeConcernMajorityJournalDefault必须为true。在
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
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.
示例
对集合取消分片
此示例将app数据库上名为inventory的集合取消分片到shard02分分片。
sh.unshardCollection( "app.inventory", "shard02" )
要获取可用分分片ID 的列表,请运行sh.status() 。 有关详细信息,请参阅sh.status() 输出。
对区域分片集合取消分片
删除区域范围
要从区域中删除范围,请使用sh.removeRangeFromZone() 方法:
sh.removeRangeFromZone( { "app.inventory", { size: 100 }, { size: 500 } } )
重复此步骤,直到从集合使用的区域中删除所有范围。
从区域中删除分片
要从区域中删除分片,运行sh.removeShardFromZone() 方法:
sh.removeShardFromZone( "shard01", "mid" )
重复此操作,直到从所有区域中删除分片。