In most circumstances, you should let the automatic balancer migrate ranges between shards. However, you may want to migrate ranges manually in a few cases:在大多数情况下,您应该让自动平衡器在分片之间迁移范围。但是,在某些情况下,您可能希望手动迁移范围:
When pre-splitting an empty collection, migrate ranges manually to distribute them evenly across the shards. Use pre-splitting in limited situations to support bulk data ingestion.在预分割空集合时,手动迁移范围以将其均匀地分布在分片上。在有限的情况下使用预拆分来支持批量数据摄入。If the balancer in an active cluster cannot distribute ranges within the balancing window, then you will have to migrate ranges manually.如果活动群集中的平衡器无法在平衡窗口内分配范围,则必须手动迁移范围。
To manually migrate ranges, use the 要手动迁移范围,请使用moveChunk or moveRange command.moveChunk或moveRange命令。
For more information on how the automatic balancer moves ranges between shards, see Balancer Internals and Range Migration.有关自动平衡器如何在分片之间移动范围的更多信息,请参阅平衡器内部和范围迁移。
Example示例
Migrate a single range迁移单个范围
The following example assumes that the field 以下示例假设字段username is the shard key for a collection named users in the myapp database, and that the value smith exists within the range to migrate. Migrate the range using the following command in mongosh.username是myapp数据库中名为users的集合的分片键,并且值smith存在于要迁移的范围内。在mongosh中使用以下命令迁移范围。
db.adminCommand( { moveChunk : "myapp.users",
find : {username : "smith"},
to : "mongodb-shard3.example.net" } )
This command moves the range that includes the shard key value "smith" to the shard named 此命令将包含分片键值“smith”的范围移动到名为mongodb-shard3.example.net. The command will block until the migration is complete.mongodb-shard3.example.net的分片。该命令将一直阻塞,直到迁移完成。
Tip
To return a list of shards, use the 要返回分片列表,请使用listShards command.listShards命令。
Example示例
Evenly migrate ranges均匀迁移范围
To evenly migrate ranges for the 要均匀迁移myapp.users collection, put each prefix range on the next shard from the other and run the following commands in the mongo shell:myapp.users集合的范围,请将每个前缀范围放在另一个分片的下一个分片上,并在mongo shell中运行以下命令:
var shServer = [ "sh0.example.net", "sh1.example.net", "sh2.example.net", "sh3.example.net", "sh4.example.net" ];
for ( var x=97; x<97+26; x++ ){
for( var y=97; y<97+26; y+=6 ) {
var prefix = String.fromCharCode(x) + String.fromCharCode(y);
db.adminCommand({moveChunk : "myapp.users", find : {email : prefix}, to : shServer[(y-97)/6]})
}
}See Create Ranges in a Sharded Cluster for an introduction to pre-splitting.有关预分割的介绍,请参阅在分片群集中创建范围。
Use the使用带有moveChunkcommand with the_secondaryThrottleandwriteConcernfields to determine when the balancer proceeds with the next document in the migrating range._secondaryThrottle和writeConcern字段的moveChunk命令来确定平衡器何时继续迁移范围内的下一个文档。Use the将moveRangecommand with thesecondaryThrottleandwriteConcernfields to determine when the balancer proceeds with the next document in the migrating range.moveRange命令与secondaryThrottle和writeConcern字段一起使用,以确定平衡器何时继续处理迁移范围内的下一个文档。
See moveChunk and moveRange for details.
Change Streams and Orphan Documents更改流和孤立文档
Starting in MongoDB 5.3, during range migration, change stream events are not generated for updates to orphaned documents.从MongoDB 5.3开始,在范围迁移期间,不会为孤立文档的更新生成更改流事件。