reshardCollection
Definição
reshardCollection
Novidades na versão 5.0.
The
reshardCollection
command changes the shard key for a collection and changes the distribution of your data.Dica
Em
mongosh
, esse comando também pode ser executado por meio do método auxiliarsh.reshardCollection()
.Os métodos auxiliares são práticos para os usuários
mongosh
, mas podem não retornar o mesmo nível de informações que os comandos do banco de dados. Nos casos em que a praticidade não for necessária ou os campos de retorno adicionais forem necessários, use o comando de banco de dados.
Compatibilidade
Esse comando está disponível em implantações hospedadas nos seguintes ambientes:
MongoDB Atlas: o serviço totalmente gerenciado para implantações do MongoDB na nuvem
Observação
Este comando é aceito em todos os clusters do MongoDB Atlas. Para obter informações sobre o suporte do Atlas a todos os comandos, consulte Comandos não suportados.
MongoDB Enterprise: a versão autogerenciada e baseada em assinatura do MongoDB
MongoDB Community: uma versão com código disponível, de uso gratuito e autogerenciada do MongoDB
Sintaxe
O comando tem a seguinte sintaxe:
db.adminCommand( { reshardCollection: "<database>.<collection>", key: <shardkey>, unique: <boolean>, numInitialChunks: <integer>, collation: { locale: "simple" }, zones: [ { min: <document with same shape as shardkey>, max: <document with same shape as shardkey>, zone: <string> | null }, ... ], forceRedistribution: <bool> } )
Campos de comando
O comando utiliza os seguintes campos:
Campo | Tipo | Descrição | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
| string | The namespace of the collection to be resharded. Takes
the form | ||||||||
| documento | The document that specifies the new field or fields to use as the chave de fragmento.
Defina os valores do campo como:
| ||||||||
| booleano | Optional. Specify whether there is a uniqueness constraint on the shard key. Only
| ||||||||
| inteiro | Optional. Specifies the initial number of chunks to create
across all shards in the cluster when resharding a collection.
The default value is | ||||||||
| documento | Optional. If the collection specified in | ||||||||
| array | Optional. Specifies the zones for the collection. To maintain or add zones, specify the zones for your collection in an array:
| ||||||||
| booleano | Optional. If set to Novidades na versão 8.0. |
Considerações
Index builds that occur during resharding might silently fail.
Do not create indexes during the resharding process.
Do not start the resharding process if there are ongoing index builds.
Resharding Process
In a collection resharding operation, a shard can be a:
donor, which currently stores chunks for the sharded collection.
recipient, which stores new chunks for the sharded collection based on the shard keys and zones.
A shard can be donor and a recipient at the same time.
The config server primary is always the resharding coordinator and starts each phase of the resharding operation.
Initialization Phase
During the initialization phase, the resharding coordinator determines the new data distribution for the sharded collection.
Clone Phase
During the clone phase:
Each recipient shard creates a temporary, empty sharded collection with the same collection options as the donor sharded collection. This new collection is the target for where recipient shards write the new data. The recipient shards do not create any index except the
_id
index until the index phase.Each recipient shard clones collection data from the donor shard, including all documents that the recipient shard owns under the new shard key.
Index Phase
During the index phase, each recipient shard 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.
Apply and Catch-up Phase
During the apply and catch-up phase:
Each recipient shard begins applying oplog entries that were written to the the corresponding donor shard after the recipient cloned the data.
When the estimate for the time remaining to complete the resharding operation is under two seconds, the donor shard blocks writes on the source collection.
Observação
If desired, you can manually force the resharding operation to
complete by issuing the commitReshardCollection
command. This is useful if the current time estimate to complete
the resharding operation is an acceptable duration for your
collection to block writes. The
commitReshardCollection
command blocks writes early
and forces the resharding operation to complete. During the time
period where writes are blocked your application experiences an
increase in latency.
Commit Phase
During the commit phase:
The resharding coordinator waits for all shards to reach strict consistency, then commits the resharding operation.
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.
Observação
Once the resharding process reaches the commit phase, the process
cannot be ended with abortReshardCollection
.
Exemplos
Refragmentar uma collection
The following example reshards the sales.orders
collection with the
new shard key { order_id: 1 }
:
db.adminCommand({ reshardCollection: "sales.orders", key: { order_id: 1 } })
Saída:
{ ok: 1, '$clusterTime': { clusterTime: Timestamp(1, 1624887954), signature: { hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0), keyId: 0 } }, operationTime: Timestamp(1, 1624887947) }
Redistribute Data to New Shards
Starting in MongoDB 8.0, you can reshard a collection on the same key, which can be used to redistribute data onto new shards.
After adding a shard to the cluster, you use the reshardCollection
command
with the forceRedistribution
option to redistribute data across the
cluster:
db.adminCommand({ reshardCollection: "accounts.invoices", key: { store_id: "hashed" }, forceRedistribution: true })
Redistribute Data to Different Zones
Starting in MongoDB 8.0, you can use the reshardCollection
command to
move data into new zones without changing the shard key.
The following command redistributes data for the accounts.sales
collection
using the same shard key, moving data to the shards associated with
zones zone04
and zone05
:
db.adminCommand({ reshardCollection: "accounts.sales", key: { region_id: "hashed" }, forceRedistribution: true, zones: [ { zone: "zone04", min: { region_id: MinKey() }, max: { region_id: 10 } }, { zone: "zone05", min: { region_id: 10 }, max: { region_id: MaxKey() } } ] })