Definition定义
moveCollectionNew in version 8.0.在版本8.0中新增。Moves a single unsharded collection to a different shard.将单个未分片集合移动到其他分片。Run在使用moveCollectionwith amongosinstance while using the admin database.admin数据库时,使用mongos实例运行moveCollection。
Compatibility兼容性
This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
Important
This command is not supported in M0 and Flex clusters. For more information, see Unsupported Commands.M0和Flex集群不支持此命令。有关详细信息,请参阅不支持的命令。
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Restrictions限制
You cannot use 您不能将moveCollection for Queryable Encryption collections.moveCollection用于可查询加密集合。
Syntax语法
The command has the following syntax:该命令具有以下语法:
db.adminCommand(
{
moveCollection: "<database>.<collection>",
toShard: "<ID of the recipient shard>",
}
)
Note
Use the 使用listShards command to retrieve the ID of the recipient shard.listShards命令检索收件人分片的ID。
Command Fields命令字段
The command takes the following fields:该命令包含以下字段:
moveCollection | ||
toShard |
Considerations注意事项
moveCollectioncan only be run on sharded clusters.只能在分片集群上运行。moveCollectioncan only move unsharded collections.只能移动未录制的集合。moveCollectioncan only move a single collection at a time.一次只能移动一个集合。moveCollectionhas a 5 minute minimum duration.最小持续时间为5分钟。MongoDB Search indexes need to be rebuilt after运行moveCollectionruns.moveCollection后,需要重建MongoDB搜索索引。You cannot make topology changes, such as add or remove shard or transition between embedded and dedicated config servers, until在moveCollectioncompletes.moveCollection完成之前,您无法进行拓扑更改,例如添加或删除分片,或在嵌入式和专用配置服务器之间进行转换。You cannot run the following operations against the collection that is being moved while当moveCollectionis in progress:moveCollection正在进行时,您无法对正在移动的集合运行以下操作:You cannot run the following operations against the cluster while当moveCollectionis in progress:moveCollection正在进行时,您无法对群集运行以下操作:Index builds that occur while在moveCollectionis in progress might silently fail.moveCollection进行过程中发生的索引构建可能会自动失败。Do not create indexes whilemoveCollectionis in progress.moveCollection正在进行时,请勿创建索引。Do not call如果有正在进行的索引构建,请不要调用moveCollectionif there are ongoing index builds.moveCollection。
Requirements需求
Before you move your collection, ensure that you meet the following requirements:在移动集合之前,请确保满足以下要求:
Your application can tolerate a period of two seconds where the affected collection blocks writes. During the time period where writes are blocked, your application experiences an increase in latency.应用程序可以容忍受影响的集合块写入的两秒钟时间。在写入被阻止的时间段内,应用程序的延迟会增加。Your database meets these resource requirements:数据库满足以下资源要求:Ensure the shard you are moving the collection to has enough storage space for the collection and its indexes. The destination shard requires at least确保将集合移动到的分片有足够的存储空间用于集合及其索引。目标分片至少需要( Collection storage size + Index Size ) * 2bytes available.( Collection storage size + Index Size ) * 2个可用字节。Ensure that your I/O capacity is below 50%.确保I/O容量低于50%。Ensure that your CPU load is below 80%.确保CPU负载低于80%。
Important
These requirements are not enforced by the database. A failure to allocate enough resources can result in:数据库不强制执行这些要求。未能分配足够的资源可能会导致:
the database running out of space and shutting down数据库空间不足并关闭decreased performance性能下降the operation taking longer than expected手术时间比预期的要长
If your application has time periods with less traffic, perform this operation on the collection during that time if possible.如果应用程序有流量较少的时间段,请在可能的情况下在该时间段内对集合执行此操作。
Example示例
This example moves an unsharded collection named 此示例将inventory on the app database to the shard02 shard.app数据库上名为inventory的未分片集合移动到shard02分片。
db.adminCommand(
{
moveCollection: "app.inventory",
toShard: "shard02"
}
)
To get a list of the available shard IDs, run 要获取可用分片ID的列表,请运行sh.status(). For details, see sh.status() Output.sh.status()。有关详细信息,请参阅sh.status()输出。