Database Manual / Reference / Database Commands / Sharding

moveCollection (database command数据库命令)

Definition定义

moveCollection

New in version 8.0.在版本8.0中新增。

Moves a single unsharded collection to a different shard. 将单个未分片集合移动到其他分片Run moveCollection with a mongos instance 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:该命令包含以下字段:

Field字段Type类型Description描述
moveCollectionstring字符串Database and name of the collection to move.要移动的集合的数据库和名称。
toShardstring字符串ID of the recipient shard.收件人分片的ID。

Considerations注意事项

  • moveCollection can only be run on sharded clusters.只能在分片集群上运行。
  • moveCollection can only move unsharded collections.只能移动未录制的集合。
  • moveCollection can only move a single collection at a time.一次只能移动一个集合。
  • moveCollection has a 5 minute minimum duration.最小持续时间为5分钟。
  • MongoDB Search indexes need to be rebuilt after moveCollection runs.运行moveCollection后,需要重建MongoDB搜索索引。
  • You cannot make topology changes, such as add or remove shard or transition between embedded and dedicated config servers, until moveCollection completes.moveCollection完成之前,您无法进行拓扑更改,例如添加或删除分片,或在嵌入式和专用配置服务器之间进行转换。
  • You cannot run the following operations against the collection that is being moved while moveCollection is in progress:moveCollection正在进行时,您无法对正在移动的集合运行以下操作:

  • You cannot run the following operations against the cluster while moveCollection is in progress:moveCollection正在进行时,您无法对群集运行以下操作:

  • Index builds that occur while moveCollection is in progress might silently fail.moveCollection进行过程中发生的索引构建可能会自动失败。

    • Do not create indexes while moveCollection is in progress.moveCollection正在进行时,请勿创建索引。
    • Do not call moveCollection if 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 ) * 2 bytes 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 sh.status(). For details, see sh.status() Output.要获取可用分片ID的列表,请运行sh.status()。有关详细信息,请参阅sh.status()输出

Learn More了解更多