定義
sh.shardCollection(namespace, key, unique, options)keyをシャードキーとして使用してコレクションをシャーディングします。シャードキーは、MongoDB がコレクションのドキュメントをシャード間でどのように分散するかを決定します。注意
バージョン6.0で変更。
MongoDB 6.0 以降では、コレクションをシャーディングする場合、最初に
sh.enableSharding()メソッドを実行してデータベースを構成する必要はありません。重要
mongosh メソッド
This page documents a
mongoshmethod. This is not the documentation for database commands or language-specific drivers, such as Node.js.データベースコマンドについては、
shardCollectionコマンドを参照してください。MongoDB API ドライバーについては、各言語の「MongoDB ドライバーのドキュメント」を参照してください。
sh.shardCollection()以下の引数を取ります。Parameterタイプ説明namespacestring
"<database>.<collection>"形式でシャーディングするコレクションの名前空間。keyドキュメント
シャードキー として使用するフィールドを指定するドキュメント。
{ <field1>: <1|"hashed">, ... }フィールド値を次のいずれかに設定します。
1、範囲ベースのシャーディング用"hashed"ハッシュされたシャードキーを指定します。
シャードキーはインデックスでサポートされている必要があります。コレクションが空でない限り、
shardCollectionコマンドの前にインデックスが存在している必要があります。コレクションが空の場合、シャードキーをサポートできるインデックスがまだ存在しない場合には、MongoDB はコレクションをシャーディングする前にインデックスを作成します。「シャードキー インデックス」も参照してください
uniqueブール値
任意。基礎のインデックスがユニーク制約を強制するようにするには、
trueを指定します。デフォルトはfalseです。ハッシュされたシャードキーを使用する場合、
trueは指定できません。For Legacy mongo Shell, you must explicitly specify the value for
uniqueif you specify theoptionsdocument.mongoshdoesn't requireuniquewhen you specify theoptionsdocument.optionsドキュメント
任意。
collationなどの任意フィールドを含むドキュメント。options引数は以下のオプションをサポートしています。Parameterタイプ説明collationドキュメント
任意。
shardCollectionに指定されたコレクションにデフォルトの照合がある場合は、``{ locale : "simple" }`` を使用して照合ドキュメントを含める必要があります。そうしないと、shardCollectionコマンドは失敗します。フィールドがシャードキー パターンをサポートするインデックスの少なくとも 1 つには、単純照合が必要です。ブール値
任意。 コレクションに定義されたゾーンとゾーン範囲に基づいて、空または存在しないコレクションの初期チャンク作成と分散を実行するには、
trueを指定します。 ハッシュされたシャーディングのみ。shardCollection()次のいずれかに当てはまる場合、presplitHashedZones: trueと はエラーを返します。シャードキーにはハッシュされたフィールドが含まれていません(単一フィールド ハッシュされたインデックスまたはハッシュされた複合インデックス ではない)。
コレクションには、定義されたゾーンまたはゾーン範囲はありません。
定義されたゾーン範囲が要件を満たしていません。
ドキュメント
任意。 新しいシャーディングされた時系列コレクションを作成するには、このオプションを指定します。
既存の時系列コレクションをシャーディングするには、このパラメーターを省略します。
shardCollectionに指定されたコレクションが時系列コレクションで、かつtimeseriesオプションが指定されていない場合、MongoDB は既存の時系列コレクションを定義する値を使用してtimeseriesフィールドに入力します。For detailed syntax, see Time Series Options.
時系列オプション
バージョン5.1の新機能。
To create a new time series collection that is sharded, specify the timeseries option to sh.shardCollection().
timeseries オプションには次のフィールドが含まれます。
互換性
このメソッドは、次の環境でホストされている配置で使用できます。
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
重要
This command is not supported in M0 and Flex clusters. For more information, see Unsupported Commands.
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
Considerations
シャードキー
シャードキーは後でも変更できますが、スケーラビリティやパフォーマンスの問題を回避するために、シャードキーの選択を慎重にすることが重要になります。
時系列コレクションのシャードキー
時系列コレクションをシャーディングする場合、シャードキーで指定できるのは次のフィールドのみです。
この
metaFieldサブフィールド
metaFieldこの
timeField
シャードキーでこれらのフィールドの組み合わせを指定できます。シャードキーのパターンでは、_id を含む他のフィールドは許可されません。
シャードキーを指定する場合には、次のいずれかです。
metaFieldまたはtimeFieldでなければなりません:シャードキーのパターン末尾
Tip
timeField だけをシャードキーとして指定することは避けてください。timeField は単調に増加するため、すべての書き込みがクラスター内の 1 つのチャンクで発生することがあります。理想的には、データをチャンク間で均等に分散します。
シャードキーを最適に選択する方法については、以下を参照してください。
ハッシュされたシャードキー
ハッシュされたシャードキーは、ハッシュされたインデックスまたはハッシュされた複合インデックスをシャードキーとして使用します。
ハッシュされたシャードキー フィールドを指定するには、 field: "hashed"の形式を使用します。
注意
ハッシュされたシャードキー コレクションの作成中にチャンクの移行が進行中の場合、 バランサー がコレクションを自動的にバランス化するまでに、初期チャンク分散が不均一になる可能性があります。
ゾーン シャーディングと初期チャンク分散
The shard collection operation (i.e. shardCollection command and the sh.shardCollection() helper) can perform initial chunk creation and distribution for an empty or a non-existing collection if zones and zone ranges have been defined for the collection. Initial chunk distribution allows for a faster setup of zoned sharding. After the initial distribution, the balancer manages the chunk distribution going forward per usual.
To shard a collection using a compound hashed index, see Initial Chunk Distribution with Compound Hashed Indexes.
複合ハッシュされたインデックスを使用した初期チャンク分散
MongoDB は、ハッシュされた複合インデックスでのコレクションのシャーディングをサポートしています。 複合ハッシュされたシャードキーを使用して、空のコレクションまたは存在しないコレクションをシャーディングする場合、MongoDB が初期チャンクの作成と分散を実行するために追加の要件が適用されます。
例については、「 空または存在しないコレクションのゾーンとゾーン範囲の事前定義」を参照してください。
Tip
一意性
unique: trueを指定する場合
If the collection is empty,
sh.shardCollection()creates the unique index on the shard key if such an index does not already exist.If the collection is not empty, you must create the index first before using
sh.shardCollection().If the collection has an index that supports the shard key but that index is not unique,
sh.shardCollection()returns an error.
シャードキーがプレフィックスであるユニーク複合インデックスを持つことは可能ですが、unique パラメーターを使用する場合、コレクションにはシャードキー上のユニークインデックスが必要です。
If you omit unique or specify unique: false, sh.shardCollection() doesn't change the collection's existing indexes. If a unique index on the shard key already exists, that index remains unique and the sharded cluster continues to enforce the uniqueness constraint on the shard key values across shards.
「シャーディングされたコレクションと一意のインデックス」も参照してください
照合
If the collection has a default collation, the sh.shardCollection() command must include a collation parameter with the value { locale: "simple" }. For non-empty collections with a default collation, you must have at least one index with the simple collation whose fields support the shard key pattern.
照合のないコレクションにはcollationオプションを指定する必要はありません。 照合のないコレクションに対して 照合オプションを指定しても、効果はありません。
書込み保証 (write concern)
mongos uses "majority" for the write concern of the shardCollection command, its helper sh.shardCollection(), and the sh.shardAndDistributeCollection() method.
バランス調整のための再シャーディング
When you run the sh.shardCollection() method, the balancer begins distributing the collection data to other shards in the cluster. A single shard can only participate in one chunk migration at a time. When MongoDB succeeds in copying a range of data from one shard to another, the range on the donor shard is marked for removal by the range deleter. This process is slow and resource intensive.
MongoDB 8.0 以降では、配置が リソース要件 を満たしている場合は、sh.shardAndDistributeCollection() メソッドを使用してコレクションをシャーディングすることをお勧めします。このメソッドは、shardCollection コマンドと reshardCollection コマンドをラップしてコレクションをシャードし、すぐに同じキーに再シャードします。これにより、 MongoDB はバランサーを待たずにシャード間でデータを再バランス化します。
詳細については、「同じシャードキーへの再シャーディング」を参照してください。
例
簡単な使い方
records という名前のデータベースに people という名前のコレクションがある場合、次のコマンドは zipcode フィールドでコレクションをシャーディングします。
sh.shardCollection("records.people", { zipcode: 1 } )
オプションで使用
The phonebook database has a collection contacts with no default collation. The following example uses sh.shardCollection() to shard the phonebook.contacts with:
last_nameフィールドのハッシュされたシャードキー、5初期チャンク、simpleの照合。
sh.shardCollection( "phonebook.contacts", { last_name: "hashed" }, false, { collation: { locale: "simple" } } )