定义
sh.moveCollection(namespace, toShard)将单个未分片集合移动到不同的分分片。
sh.moveCollection()mongos使用 管理员数据库 时,通过 实例运行 。重要
mongosh 方法
This page documents a
mongoshmethod. This is not the documentation for database commands or language-specific drivers, such as Node.js.有关数据库命令,请参阅
moveCollection命令。如需了解 MongoDB API 驱动程序,请参阅特定语言的 MongoDB 驱动程序文档。
语法
sh.moveCollection() 通过以下语法实现:
sh.moveCollection( "<namespace>", "<toShard>" )
注意
使用 listShards 命令检索接收分片的ID 。
参数
sh.moveCollection() 使用以下参数:
Parameter | 类型 | 说明 |
|---|---|---|
| 字符串 | 要移动的数据库和集合名称。 |
| 字符串 | 接收分片的 ID。 |
兼容性
此方法可用于以下环境中托管的部署:
- MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
注意
此命令在Atlas免费和 Flex 层级上不可用。
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
Considerations
sh.moveCollection()只能在分分片的集群上运行。sh.moveCollection()只能移动未分片的集合。sh.moveCollection()一次只能移动一个集合。sh.moveCollection()最短持续时间为5分钟。writeConcernMajorityJournalDefault必须为true。如果要移动的集合使用Atlas Search,则其搜索索引在
moveCollection完成后将不可用。您必须手动重建搜索索引。If the collection you're moving is archived in Atlas Online Archives, the online archive files are marked as
Orphanedonce the moving 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.在
moveCollection完成之前,您无法进行拓扑结构更改,例如添加或删除分片或在嵌入式和专用配置服务器之间进行转换。当
sh.moveCollection()正在进行时,您无法对正在移动的集合运行以下操作:moveCollection正在进行时,您无法对集群运行以下操作:在
sh.moveCollection()正在进行时进行的索引构建可能会静默失败。正在进行
sh.moveCollection()时,请勿创建索引。如果正在进行索引构建,请勿调用
sh.moveCollection()。
要求
在移动集合之前,请确保满足以下要求:
您的应用程序可以允许受影响的集合块进行两秒钟的写入。 在写入受阻期间,应用程序的延迟会增加。
您的数据库符合这些资源要求:
确保分片集合移动到的分片具有足够的存储空间用于集合及其索引。 目标分分片需要至少
( Collection storage size + Index Size ) * 2字节可用。确保 I/O容量低于50 %。
确保 CPU 负载低于80 %。
重要
数据库不会强制执行这些要求。未能分配足够的资源可能会导致:
数据库空间不足并关闭
性能下降
操作花费的时间比预期长
如果应用程序存在流量较少的时间段,请尽可能在该时间段对集合执行此操作。
示例
此示例将app数据库上名为inventory的未分片集合移动到shard02分分片。
sh.moveCollection( "app.inventory", "shard02" )
要获取可用分分片ID 的列表,请运行sh.status() 。 有关详细信息,请参阅sh.status() 输出。