定义
moveChunkInternal administrative command. Moves chunks between shards. Issue the
moveChunkcommand via amongosinstance while using the admin database. Use the following forms:提示
In
mongosh, this command can also be run through thesh.moveChunk()helper method.Helper methods are convenient for
mongoshusers, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.db.adminCommand( { moveChunk : <namespace> , find : <query> , to : <ID of the recipient shard>, forceJumbo: <boolean>, _secondaryThrottle : <boolean>, writeConcern: <document>, _waitForDelete : <boolean> } ) 或者:
db.adminCommand( { moveChunk : <namespace> , bounds : <array> , to : <ID of the recipient shard>, forceJumbo: <boolean>, _secondaryThrottle : <boolean>, writeConcern: <document>, _waitForDelete : <boolean> } ) 注意
使用
listShards命令检索接收分片的ID 。The
moveChunkcommand has the following fields:字段类型说明moveChunk字符串
数据块 所在 collection的 命名空间 。指定集合的完整命名空间,包括数据库名称。
find文档
分片键的等值匹配,用于指定要移动的数据段的分片键值。 指定
bounds字段或find字段,但不能同时指定两者。 请勿使用find字段来选择使用哈希分片键的集合中的数据段。bounds阵列
要移动的特定数据块的边界。该数组必须由两个文档组成,这两个文档指定要移动的数据段的下分片键值和上分片键值。 指定
bounds字段或find字段,但不能同时指定两者。 使用bounds选择使用哈希分片键的collection中的数据块。to字符串
数据块的接收分片片的ID 。
布尔
可选。 一个标志,用于确定该命令是否可以移动过大而无法迁移的数据块。该数据段可能会也可能不会标记为jumbo。
如果为
true,则该命令可以移动数据块。如果为
false,则该命令无法移动数据块。
默认为
false。警告:
附带
forceJumbo=true的moveChunk命令会阻止集合上的写入操作。此选项会导致分片迁移数据段,即使数据段大于配置的数据块大小。 在迁移期间,该集合仍然不可写入。
要在没有这么长的阻塞期的情况下迁移这些大数据段,请参阅平衡超出大小限制的范围。
_secondaryThrottle布尔
可选。 对于WiredTiger ,默认为
false。如果
true,则默认情况下,数据段迁移期间的每个文档移动都会传播到至少一个从节点,然后负载均衡器才会继续处理下一个文档。 这相当于 写关注(write{ w: 2 }concern)的。使用
writeConcern选项指定不同的写关注(write concern)。如果为
false,负载均衡器不会等待复制到从节点,而是继续处理下一个文档。
有关更多信息,请参阅辅助限制。
writeConcern文档
可选。 一个表达写关注(write concern)的文档,在数据块迁移期间,
_secondaryThrottle将使用该写关注(write concern)来等待从节点。writeConcern需要_secondaryThrottle: true。_waitForDelete布尔
Optional. If set to
true, the delete phase of amoveChunkoperation is blocking._waitForDeletedefaults tofalse.当
_waitForDelete字段设立为true时, MongoDB在执行范围删除之前不会等待orphanCleanupDelaySecs延迟。如果您使用_waitForDelete参数并在从节点上执行任何读取操作,则读取可能会由于迁移的删除阶段而终止。要学习;了解详情,请参阅terminateSecondaryReadsOnOrphanCleanup。bounds的值采用以下形式:[ { hashedField : <minValue> } , { hashedField : <maxValue> } ] 数据块迁移部分描述了数据块如何在 MongoDB 上的分片之间移动。
兼容性
此命令可用于以下环境中托管的部署:
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
Considerations
Only use the moveChunk in special circumstances such as preparing your sharded cluster for an initial ingestion of data, or a large bulk import operation. In most cases allow the balancer to create and balance chunks in sharded clusters. See Create Ranges in a Sharded Cluster for more information.
行为
索引
moveChunk requires that all indexes exist on the target (i.e. to ) shard before migration and returns an error if a required index does not exist.
元数据错误
moveChunk returns the following error message if another metadata operation is in progress on the chunks collection:
errmsg: "The collection's metadata lock is already taken."
If another process, such as a balancer process, changes meta data while moveChunk is running, you may see this error. You may retry the moveChunk operation without side effects.
maxCatchUpPercentageBeforeBlockingWrites 服务器参数
Starting in MongoDB 5.0, you can set the maxCatchUpPercentageBeforeBlockingWrites to specify the maximum allowed percentage of data not yet migrated during a moveChunk operation when compared to the total size (in MBs) of the chunk being transferred.