Docs Menu

sh.reshardCollection()

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

バージョン 5.0 で追加

The sh.reshardCollection() method changes the shard key for a collection and changes the distribution of your data.

Before you reshard a collection, read the the reshard requirements and reshard limitations.

重要

mongosh メソッド

このページでは、mongosh メソッドについて記載しています。ただし、データベースコマンドや Node.js などの言語固有のドライバーのドキュメントには該当しません

データベースコマンドについては、reshardCollection コマンドを参照してください。

MongoDB API ドライバーについては、各言語の MongoDB ドライバー ドキュメントを参照してください。

sh.reshardCollection()は次のフィールドを取ります。

フィールド
タイプ
説明

namespace

string

"<database>.<collection>"形式でシャーディングするコレクションの名前空間

key

ドキュメント

シャードキーとして使用する新しいフィールドを指定するドキュメント。

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

フィールド値を次のいずれかに設定します。

シャードキー インデックス」も参照してください

unique

ブール値

任意。 シャードキーに一意の制約があるかどうかを指定します。 falseのみがサポートされています。 デフォルトはfalseです。

options

ドキュメント

Optional. A document containing optional fields, including numInitialChunks, collation, zones and forceRedistribution.

The options field supports the following fields:

フィールド
タイプ
説明

numInitialChunks

integer

任意。 コレクションを 再シャーディングする ときに、クラスター内のすべてのシャードにわたって作成するチャンクの初期数を指定します。 デフォルト値は 90 です。 次に、 MongoDB はクラスター全体でチャンクを作成し、バランスをとります。 numInitialChunks の結果は、シャードあたり 8192 未満である必要があります。

If you see the error "The shard key provided does not have enough cardinality to make the required number of chunks", reshard the collection with a lower numInitialChunks value.

collation

ドキュメント

Optional. If the collection specified to reshardCollection has a default collation, you must include a collation document with { locale : "simple" }, or the reshardCollection command fails.

zones

配列

任意。 コレクションの ゾーン を指定します。

ゾーン を維持または追加するには、コレクションの ゾーン を以下のように配列で指定します。

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

forceRedistribution

ブール値

Optional. If set to true, the operation runs even if the new shard key is the same as the old shard key. Use with the zones option to move data to specific zones.

バージョン8.0の新機能

このメソッドは、次の環境でホストされている配置で使用できます。

  • MongoDB Atlas はクラウドでの MongoDB 配置のためのフルマネージド サービスです

重要

このコマンドは、M0、M2、M5、および Flex クラスターではサポートされていません。詳細については、「 サポートされていないコマンド 」を参照してください。

  • MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン

  • MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン

リシャーディング中に発生するインデックスビルドが、暗黙で失敗する可能性があります。

  • リシャーディング プロセスの実行中はインデックスを作成しないでください。

  • インデックスのビルドが進行中の場合は、リシャーディングプロセスを開始しないでください。

コレクションの再シャーディング操作では、シャードは次のようになります。

シャードは同時にドナーでも受信者でもあることができます。

コンフィギュレーションサーバーのプライマリは常にリシャーディング コーディネーターであり、リシャーディング操作の各フェーズを開始します。

初期化フェーズ中に、リシャーディング コーディネーターはシャーディングされたコレクションの新しいデータ分散を決定します。

クローン フェーズ実行中:

  • 各受信者シャードは、ドナーのシャーディングされたコレクションと同じコレクションオプションを持つ一時的な空のシャーディングされたコレクションを作成します。 この新しいコレクションは、受信者シャードが新しいデータを書き込むターゲットです。 受信者シャードは、インデックスフェーズまで、_idインデックス以外のインデックスを作成しません。

  • 各受信者シャードは、新しいシャードキーの下で受信者シャードが所有するすべてのドキュメントを含む、ドナー シャードからのコレクションデータを複製します。

インデックスフェーズでは、各受信者シャードは必要な新しいインデックスをビルドします。 これらには、シャーディングされたコレクションの既存のすべてのインデックスと、新しいシャードキーパターン と互換性のあるインデックス(シャーディングされたコレクションにインデックスがまだ存在しない場合)が含まれます。

適用フェーズとキャッチアップフェーズ中:

  • 各受信者シャードは、受信者がデータをクローンした後に対応するドナー シャードに書込まれたoplogエントリの適用を開始します。

  • リシャーディング操作を完了するための残り時間が 2 秒未満の場合、ドナー シャードはソースコレクションへの書込みをブロックします。

注意

If required, you can manually force the resharding operation to complete by issuing the sh.commitReshardCollection() method. 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. During the time period where writes are blocked your application experiences an increase in latency.

コミット フェーズ実行中:

  • リシャーディング コーディネーターは、すべてのシャードが厳密な一貫性に達するまで待機し、 リシャーディング操作 をコミットします。

  • リシャーディング コーディネーターは、各ドナー シャード プライマリと受信者シャード プライマリに対して、一時的にシャーディングされたコレクションの名前を変更するよう、個別に指示します。 一時的なコレクションは、新しいリシャーディングされたコレクションになります。

  • 各ドナー シャードは、古いシャーディングされたコレクションを削除します。

注意

リシャーディング プロセスがコミット フェーズに達すると、sh.abortReshardCollection() ではプロセスを終了できなくなります。

次の例では、 sales.ordersコレクションを新しいシャードキー{ order_id: 1 }で再シャーディングします。

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

出力例:

{
ok: 1,
'$clusterTime': {
clusterTime: Timestamp(1, 1624887954),
signature: {
hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
keyId: 0
}
},
operationTime: Timestamp(1, 1624887947)
}

In order to reshard to the same shard key, set forceRedistribution to true. The following example reshards the sales.orders collection to the same shard key { order_id: 1 } and redistributes data.

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

出力例:

{
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1733502241, i: 20 }),
signature: {
hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),
keyId: Long('0')
}
},
operationTime: Timestamp({ t: 1733502241, i: 20 })
}

Reshard a collection with zones when you need to adjust the distribution of data across the shards in your cluster to meet changing requirements or to improve performance.

In the following example, the test.scores collection resides on shard0 and shard1. The current shard key is { _id: 1}.

1

In this example, this zone is called NewZone.

sh.addShardToZone( "shard2", "NewZone" )
sh.addShardToZone( "shard3", "NewZone" )
2
sh.reshardCollection(
"test.scores",
{ "studentId": 1, "testId": 1},
{ zones: [ {
min: { "studentId": MinKey(), "testId": MinKey() },
max: { "studentId": MaxKey(), "testId": MaxKey() },
zone: "NewZone" }
]
} )

The resharding operation adds the shards in zone NewZone as recipients. The database primary shard is added as a recipient as a backstop for any missing ranges in the zone definition. If there are no missing ranges, the collection is cloned on shards in the "NewZone", such as shard2 and shard3 in this example. sh.reshardCollection returns the following:

{
ok: 1,
'$clusterTime': {
clusterTime: Timestamp( { t: 1699484530, i: 54 } ),
signature: {
hash: Binary.createFromBase64( "90ApBDrSSi4XnCpV3OWIH4OGO0Y=", 0 ),
keyId: Long( "7296989036055363606" )
} },
operationTime: Timestamp( { t: 1699484530, i: 54 } )
}