sh.reshardCollection()

On this page本页内容

Definition定义

sh.reshardCollection(namespace, key, unique, options)

New in version 5.0.在版本5.0中新增

The sh.reshardCollection() method changes the shard key for a collection and changes the distribution of your data.sh.reshardCollection()方法更改集合的分片键并更改数据的分布。

sh.reshardCollection() takes the following arguments:采用以下参数:

Parameter参数Type类型Description描述
namespacestringThe namespace of the collection to shard in the form "<database>.<collection>". 要分片的集合的名称空间,格式为"<database>.<collection>"
keydocument

The document that specifies the new field or fields to use as the shard key.指定要用作分片键的一个或多个新字段的文档。

{ <field1>: <1|"hashed">, ... }

Set the field values to either:将字段值设置为:

See also Shard Key Indexes另请参见分片键索引

uniquebooleanOptional. 可选。Specify whether there is a uniqueness constraint on the shard key. 指定分片键上是否有唯一性约束。Only false is supported. 仅支持falseDefaults to false.默认为false
optionsdocumentOptional. 可选。A document containing optional fields, including numInitialChunks, collation and zones.包含可选字段的文档,包括numInitialChunkscollationzones

The options argument supports the following options:options参数支持以下选项:

Parameter参数Type类型Description描述
numInitialChunksintegerOptional. 可选。Specifies the initial number of chunks to create across all shards in the cluster when resharding a collection. 指定重新存储集合时要跨群集中所有分片创建的块的初始数量。The default is the number of chunks that exist for the collection under the current shard key pattern. 默认值是当前分片键模式下集合存在的块数。MongoDB will then create and balance chunks across the cluster. 然后,MongoDB将在集群中创建和平衡块。The numInitialChunks must result in less than 8192 per shard. numInitialChunks必须导致每个分片小于8192
collationdocumentOptional. 可选。If the collection specified to reshardCollection has a default collation, you must include a collation document with { locale : "simple" }, or the reshardCollection command fails. 如果指定给reshardCollection的集合具有默认排序规则,则必须包含带有{ locale : "simple" }的排序规则文档,否则reshardCollection命令将失败。
zonesarray

Optional. 可选。To maintain or add zones, specify the zones for your collection in an array:要维护或添加分区,请在数组中指定集合的分区:

[
   {
       min: <document with same shape as shardkey>,
       max: <document with same shape as shardkey>,
       zone: <string> | null
   },
   ...
]

Resharding Process重新硬化过程

During the resharding process, there are two roles a shard may play:在重新存储过程中,分片可能扮演两个角色:

  • Donors are shards which currently own chunks of the sharded collection.捐赠者是目前拥有分片集合块的分片。
  • Recipients are shards which would own chunks of the sharded collection according to the new shard key and zones.接收者是分片,根据新的分片键和区域,它们将拥有分片集合的块。

A shard may play both the role of a donor and a recipient concurrently. 分片可以同时扮演捐赠者和接收者的角色。Unless zones are being used, the set of donor shards is the same as the set of recipient shards.除非使用区域,否则捐赠者分片集与接收者分片集相同。

The config server primary is always chosen as the resharding coordinator, responsible for initiating each phase of the process.配置服务器主服务器总是被选为重新分配协调器,负责启动流程的每个阶段。

Initialization Phase初始化阶段

During the initialization phase:在初始化阶段:

  • The balancer determines the new data distribution for the sharded collection.平衡器确定分片集合的新数据分布。

Index Phase索引阶段

During the index phase:在索引阶段:

  • Each shard recipient creates a new, empty sharded collection with the same collection options as the existing sharded collection. 每个分片接收者创建一个新的空分片集合,该集合具有与现有分片集合相同的集合选项。This new sharded collection is the target for where recipient shards write the new data.这个新的分片集合是收件人分片写入新数据的目标。
  • Each shard recipient builds the necessary new indexes. 每个分片接收者构建必要的新索引。These include all existing indexes on the sharded collection and an index compatible with the new shard key pattern if such an index doesn’t already exist on the sharded collection.这些包括分片集合上的所有现有索引,以及与新的分片键模式兼容的索引(如果分片集合中不存在这样的索引)。

Clone, Apply, and Catch-up Phase克隆、应用和赶上阶段

During the clone, apply, and catch-up phase:在克隆、应用和追赶阶段:

  • Each shard recipient clones an initial copy of the documents it would own under the new shard key.每个分片接收者克隆一个其在新分片键下拥有的文档的初始副本。
  • Each shard recipient begins applying oplog entries from operations that happened after the recipient cloned the data.每个分片接收者都会从其克隆数据后发生的操作中开始应用oplog条目。
  • When the estimate for the time remaining to complete the resharding operation is under two seconds, the resharding coordinator blocks writes for the collection.当完成重新装载操作的剩余时间估计在两秒以下时,重新装载协调器将阻止对集合的写入。

    Note注意

    If desired, you can manually force the resharding operation to complete by issuing the sh.commitReshardCollection() method. 如果需要,可以通过发出sh.commitReshardCollection()方法手动强制完成重新存储操作。This is useful if the current time estimate to complete the resharding operation is an acceptable duration for your collection to block writes. 如果完成重新存储操作的当前估计时间是集合阻止写入的可接受持续时间,则此选项非常有用。The sh.commitReshardCollection() method blocks writes early and forces the resharding operation to complete. sh.commitReshardCollection()方法会阻止提前写入,并强制完成重新存储操作。During the time period where writes are blocked your application experiences an increase in latency.在写操作被阻止的时间段内,应用程序的延迟会增加。

Commit Phase提交阶段

  • Once the resharding process reaches the commit phase, it may no longer be aborted with sh.abortReshardCollection().一旦重新存储进程到达提交阶段,就不能再使用sh.abortReshardCollection()中止它。
  • When all shards have reached strict consistency, the resharding coordinator commits the resharding operation and installs the new routing table.当所有分片都达到严格的一致性时,重新存储协调器将提交重新存储操作并安装新的路由表。
  • The resharding coordinator instructs each donor and recipient shard primary, independently, to rename the temporary sharded collection. 重新存储协调器分别指示每个捐赠者和接收者分片主项重命名临时分片集合。The temporary collection becomes the new resharded collection.临时集合成为新的重新存储集合。
  • Each donor shard drops the old sharded collection.每个捐赠分片都会丢弃旧的分片集合。

    Tip提示

Example示例

Reshard a Collection重新扫描集合

The following example reshards the sales.orders collection with the new shard key { order_id: 1 }:以下示例使用新的分片键{ order_id: 1 }重新划分sales.orders集合:

sh.reshardCollection("sales.orders", { order_id: 1 })

MongoDB returns the following:MongoDB返回以下内容:

{
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp(1, 1624887954),
    signature: {
      hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
      keyId: 0
    }
  },
  operationTime: Timestamp(1, 1624887947)
}
Tip提示
←  sh.removeShardFromZone()sh.setBalancerState() →