定義
refineCollectionShardKeyコレクションのシャードキーを変更するために、新しいフィールドをサフィックスとして既存のキーに追加します。 コレクションのシャードキーを調整すると、濃度が十分でないために既存のキーがジャンボ (分割できない)チャンクになる状況に対処できます。
注意
データ分散
As part of refining the shard key, the refineCollectionShardKey command updates the chunk ranges and zone ranges to incorporate the new fields without modifying the range values of the existing key fields. That is, the refinement of the shard key does not immediately affect the distribution of chunks across shards or zones. Any future chunk splits or migration occur as part of the routine sharding operations.
互換性
このコマンドは、次の環境でホストされている配置で使用できます。
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
重要
This command is not supported in M10+ clusters. For more information, see Unsupported Commands.
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
構文
注意
To use the refineCollectionShardKey command, the sharded cluster must have feature compatibility version (fcv) of 4.4.
このコマンドの構文は、次のとおりです。
db.adminCommand( { refineCollectionShardKey: "<database>.<collection>", key: { <existing key specification>, <suffix1>: <1|"hashed">, ... } } )
コマンドフィールド
このコマンドは、次のフィールドを使用します。
フィールド | タイプ | 説明 |
|---|---|---|
string |
| |
ドキュメント | コレクションの新しいシャードキーとして使用するフィールドを指定するドキュメント。
サフィックス フィールドの場合は、フィールド値を次のいずれかに設定します。
|
Tip
アクセス制御
アクセス制御を使用して実行中の場合、コマンドを実行するにはrefineCollectionShardKey データベースやコレクション に対する 特権アクションが必要です。つまり、ユーザーは、次の 特権 を付与する ロール を持っていなければなりません。
{ resource: { db: <database>, collection: <collection> }, actions: [ "refineCollectionShardKey" ] }
組み込みの clusterManager ロールにより、適切な権限が付与されます。
Considerations
インデックスの考慮事項
- インデックスの存在
An index that supports the command's specified key must exist prior to running the command.
サポート インデックスは、新しいシャードキー仕様で開始されるインデックスです。つまり、インデックス プレフィックスが新しいシャードキー仕様と一致する。 つまり、シャードキーを
{ x: 1, y: 1 }{ x: 1 }に変更するには、 かつ{ x: 1, y: 1 }で始まるインデックスが存在する必要があります。例:{ x: 1, y: 1 }{ x: 1, y: 1, a: 1, b: 1}
注意
サポートするインデックスを部分インデックスにすることはできません。
サポートインデックスはsparse indexにできません。
コレクションが
simple以外の照合を使用する場合、サポート インデックスは{ locale: "simple" }照合を指定する必要があります。
- ユニークインデックス
現在のシャード インデックスに一意の制約がある場合、新しいシャードキー インデックスにも一意の制約が必要です。
After creating the unique index to support the new shard key, drop the old shard key index before running
refineCollectionShardKey.また、現在のシャードインデックスに一意の制約がある場合、新しいシャードキーはそのフィールドのいずれにも
"hashed"を指定できません。「シャーディングされたコレクションと一意のインデックス」も参照してください。
- インデックス照合
- シャーディングされたコレクションのデフォルトの照合が
simple以外の場合、インデックスには{ locale : "simple" }の照合ドキュメントが含まれている必要があります。 フィールドがシャードキー パターンをサポートするインデックスの少なくとも 1 つには、単純照合が必要です。
警告
現在のシャードキー フィールドの範囲またはハッシュされたタイプは変更 しない でください。 データの不整合が発生します。 たとえば、シャードキーを{ customer_id: 1 }から{ customer_id: "hashed", order_id: 1 }に変更 しないでください 。
例
testデータベースで例を設定するには、次の手順に従います。
次の
shardCollection操作を使用して、testデータベース内のordersコレクションをシャーディングします。 この操作では、customer_idフィールドを初期シャードキーとして使用します。db.adminCommand( { shardCollection: "test.orders", key: { customer_id: 1 } } )
シャードキーをcustomer_idフィールドとorder_idフィールド{ customer_id: 1, order_id: 1 }に変更するには、
Create the indexインデックスがまだ存在しない場合に新しいシャードキーをサポートするため。db.getSiblingDB("test").orders.createIndex( { customer_id: 1, order_id: 1 } ) Run
refineCollectionShardKeycommand to add theorder_idfield as a suffix:db.adminCommand( { refineCollectionShardKey: "test.orders", key: { customer_id: 1, order_id: 1 } } )
コマンドが正常に完了すると、コレクションのシャードキーは{ customer_id: 1, order_id: 1 }に変更されました。 確認するには、 sh.status()を実行します。
Tip
シャードキーを調整すると、コレクション内のすべてのdocumentにサフィックスフィールドが存在しない場合があります。欠落しているシャードキーフィールドを入力するには、欠落しているシャードキー フィールドの設定を参照してください。
シャードキーを検証する前に、コレクション内のすべてまたはほとんどのドキュメントにサフィックス フィールドが含まれていることを確認して、可能であれば、フィールドにその後にフィールドを入力する必要がないようにします。
以外の照合順序を持つコレクションsimple
testデータベースで例を設定するには、次の手順に従います。
フランス語の
frをデフォルトの照合として指定して、testデータベースにcafésコレクションを作成します。db.getSiblingDB("test").createCollection( "cafés", { collation: { locale: "fr" } } ); customer_idフィールドを初期シャードキーとして使用してコレクションをシャーディングします。 コレクションにはデフォルトのfr照合があり、simple照合がないため、shardCollectionコマンドにはcollation: { locale: "simple" }オプションを含める必要があります。db.adminCommand( { shardCollection: "test.cafés", key: { customer_id: 1 }, collation: { locale: "simple" } } )
シャードキーを変更して、 customer_idフィールドとorder_idフィールド{ customer_id: 1, order_id: 1 }の両方を指定するには、
インデックスがまだ存在しない場合に新しいシャードキーをサポートするための
Create the indexコレクションは非単純な照合を使用するため、インデックスにはcollation: { locale: "simple" }オプションを含める必要があります。db.getSiblingDB("test").cafés.createIndex( { customer_id: 1, order_id: 1 }, { collation: { locale: "simple" } } ) Run
refineCollectionShardKeycommand to add theorder_idfield as a suffix:db.adminCommand( { refineCollectionShardKey: "test.cafés", key: { customer_id: 1, order_id: 1 } } )
コマンドが正常に完了すると、コレクションのシャードキーは{ customer_id: 1, order_id: 1 }に変更されました。 確認するには、 sh.status()を実行します。
Tip
シャードキーを調整すると、コレクション内のすべてのdocumentにサフィックスフィールドが存在しない場合があります。欠落しているシャードキーフィールドを入力するには、欠落しているシャードキー フィールドの設定を参照してください。
シャードキーを検証する前に、コレクション内のすべてまたはほとんどのドキュメントにサフィックス フィールドが含まれていることを確認して、可能であれば、フィールドにその後にフィールドを入力する必要がないようにします。
Tip