Docs HomeMongoDB Manual

sh.moveChunk()

On this page本页内容

Definition定义

sh.moveChunk(namespace, query, destination)

Moves the chunk that contains the document specified by the query to the destination shard. 将包含query指定的文档的区块移动到destination分片。sh.moveChunk() provides a wrapper around the moveChunk database command.提供了moveChunk数据库命令的包装器。

Important

In most circumstances, allow the balancer to automatically migrate chunks, and avoid calling sh.moveChunk() directly.在大多数情况下,允许平衡器自动迁移,并避免直接调用sh.moveChunk()

Warning

This command cannot be used for hashed shard keys, as it uses the find option from the moveChunk command. 该命令不能用于哈希分片键,因为它使用了moveChunk命令中的find选项。To move a chunk with a hashed shard key, use the moveChunk command specifying the bounds field.要使用哈希分片键移动区块,请使用指定bounds字段的moveChunk命令。

Important

mongosh Method

This page documents a mongosh method. This is not the documentation for database commands or language-specific drivers, such as Node.js.

For the database command, see the moveChunk command.

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.

For the legacy mongo shell documentation, refer to the documentation for the corresponding MongoDB Server release:

mongo shell v4.4

sh.moveChunk() takes the following arguments:接受以下参数:

Parameter参数Type类型Description描述
namespacestringThe namespace of the sharded collection that contains the chunk to migrate.包含要迁移的区块的分片集合的命名空间
querydocumentAn equality match on the shard key that selects the chunk to move.选择要移动的区块的分片键上的相等匹配。
destinationstringThe name of the shard to move.要移动的分片的名称。

By default, MongoDB cannot move a chunk if the number of documents in the chunk is greater than 1.3 times the result of dividing the configured chunk size by the average document size. 默认情况下,如果区块中的文档数量大于配置的区块大小除以平均文档大小的1.3倍,MongoDB将无法移动区块。Starting in MongoDB 4.4, the moveChunk command can specify a new option forceJumbo to allow for the manual migration of chunks too large to move, with or without the jumbo label. 从MongoDB 4.4开始,moveChunk命令可以指定一个新选项forceJumbo,以允许手动迁移太大而无法移动的块,无论是否使用jumbo标签。See moveChunk command for details.有关详细信息,请参阅moveChunk命令。

Example实例

Given the people collection in the records database, the following operation finds the chunk that contains the documents with the zipcode field set to 53187 and then moves that chunk to the shard named shard0019:给定records数据库中的people集合,以下操作会找到包含zipcode字段设置为53187的文档的区块,然后将该区块移动到名为shard0019的分片:

sh.moveChunk("records.people", { zipcode: "53187" }, "shard0019")