AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

refineCollectionShardKey (データベースコマンド)

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

"<database>.<collection>"形式のシャーディングされたコレクションの名前空間。

ドキュメント

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

{ <existing key specification>, <suffix1>: <1|"hashed">, ... }

  • 新しいキーは、既存のシャードキーで開始する必要があります。

  • The new key must be supported by an index that must exist before you run the refineCollectionShardKey command. See Index Considerations for more information.

サフィックス フィールドの場合は、フィールド値を次のいずれかに設定します。

アクセス制御を使用して実行中の場合、コマンドを実行するにはrefineCollectionShardKey データベースやコレクション に対する 特権アクションが必要です。つまり、ユーザーは、次の 特権 を付与する ロール を持っていなければなりません。

{ resource: { db: <database>, collection: <collection> }, actions: [ "refineCollectionShardKey" ] }

組み込みの clusterManager ロールにより、適切な権限が付与されます。

  • インデックスの存在

    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}

    注意

  • ユニークインデックス

    現在のシャード インデックスに一意の制約がある場合、新しいシャードキー インデックスにも一意の制約が必要です。

    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データベースで例を設定するには、次の手順に従います。

  1. 次のshardCollection操作を使用して、 testデータベース内のordersコレクションをシャーディングします。 この操作では、 customer_idフィールドを初期シャードキーとして使用します。

    db.adminCommand( { shardCollection: "test.orders", key: { customer_id: 1 } } )

シャードキーをcustomer_idフィールドとorder_idフィールド{ customer_id: 1, order_id: 1 }に変更するには、

  1. Create the index インデックスがまだ存在しない場合に新しいシャードキーをサポートするため。

    db.getSiblingDB("test").orders.createIndex( { customer_id: 1, order_id: 1 } )
  2. Run refineCollectionShardKey command to add the order_id field 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にサフィックスフィールドが存在しない場合があります。欠落しているシャードキーフィールドを入力するには、欠落しているシャードキー フィールドの設定を参照してください。

シャードキーを検証する前に、コレクション内のすべてまたはほとんどのドキュメントにサフィックス フィールドが含まれていることを確認して、可能であれば、フィールドにその後にフィールドを入力する必要がないようにします。

testデータベースで例を設定するには、次の手順に従います。

  1. フランス語のfrをデフォルトの照合として指定して、 testデータベースにcafésコレクションを作成します。

    db.getSiblingDB("test").createCollection( "cafés", { collation: { locale: "fr" } } );
  2. 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 }の両方を指定するには、

  1. インデックスがまだ存在しない場合に新しいシャードキーをサポートするためのCreate the index コレクションは非単純な照合を使用するため、インデックスにはcollation: { locale: "simple" }オプションを含める必要があります。

    db.getSiblingDB("test").cafés.createIndex(
    { customer_id: 1, order_id: 1 },
    { collation: { locale: "simple" } }
    )
  2. Run refineCollectionShardKey command to add the order_id field 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にサフィックスフィールドが存在しない場合があります。欠落しているシャードキーフィールドを入力するには、欠落しているシャードキー フィールドの設定を参照してください。

シャードキーを検証する前に、コレクション内のすべてまたはほとんどのドキュメントにサフィックス フィールドが含まれていることを確認して、可能であれば、フィールドにその後にフィールドを入力する必要がないようにします。