sh.shardAndDistributeCollection()
定義
sh.shardAndDistributeCollection(namespace, key, unique, options)
Shards a collection and immediately redistributes the data using the provided シャードキー. The immediate redistribution of data allows for faster data movement and reduced impact to workloads.
重要
mongosh メソッド
このページでは、
mongosh
メソッドについて記載しています。これは Node.js などの言語固有のドライバーのドキュメントではありません。MongoDB API ドライバーについては、各言語の MongoDB ドライバー ドキュメントを参照してください。
Running
sh.shardAndDistributeCollection()
inmongosh
has the same result as consecutively running theshardCollection
andreshardCollection
commands.
パラメーター
sh.shardAndDistributeCollection()
は次のパラメータを取ります。
Parameter | タイプ | 必要性 | 説明 |
---|---|---|---|
| 文字列 | 必須 | The namespace of the collection to shard in the format
|
| ドキュメント | 必須 | シャードキー として使用するフィールドを指定するドキュメント。
フィールド値を次のいずれかに設定します。
以下も参照してください。 シャードキー インデックス |
| ブール値 | 任意 | 基礎のインデックスが一意の制約を強制するようにするには、 When using hashed shard keys, you can't
specify |
| ドキュメント | 任意 | A document containing optional fields, including
|
options
引数は以下のオプションをサポートしています。
Parameter | タイプ | 説明 |
---|---|---|
| 整数 | Specifies the initial number of chunks to create across all shards in
the cluster when sharding and resharding a collection. MongoDB then
creates and balances chunks across the cluster. The
|
| ドキュメント | If the collection specified to |
| ブール値 | Specify
|
| ドキュメント | Specify this option to create a new sharded time series collection. 既存の時系列コレクションをシャーディングするには、このパラメーターを省略します。 When the collection specified to 詳細な構文については「時系列オプション 」を参照してください。 |
Considerations
The following factors can impact performance or the distribution of your data.
シャードキー
Although you can change your shard key later, carefully consider your shard key choice to optimize scalability and perfomance.
時系列コレクションのシャードキー
時系列コレクションをシャーディングする場合、シャードキーで指定できるのは次のフィールドのみです。
metaField
metaField
のサブフィールドtimeField
シャードキーでこれらのフィールドの組み合わせを指定できます。シャードキーのパターンでは、_id
を含む他のフィールドは許可されません。
シャードキーを指定する場合には、次のいずれかです。
metaField
は次のいずれかになります。timeField
は次の条件を満たす必要があります。シャードキーのパターン末尾
Tip
timeField
だけをシャードキーとして指定することは避けてください。
timeField
は単調に増加するため、すべての書き込みがクラスター内の 1 つのチャンクで発生することがあります。理想的には、データをチャンク間で均等に分散します。
シャードキーを最適に選択する方法については、以下を参照してください。
以下も参照してください。
ハッシュされたシャードキー
ハッシュされたシャードキーは、ハッシュされたインデックスまたはハッシュされた複合インデックスをシャードキーとして使用します。
To specify a hashed shard key field, use 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.
For an example, see 空、または存在しないコレクションのゾーンとゾーン範囲の事前定義. If sharding a
collection using a ranged or single-field hashed shard key, the
numInitialChunks
option has no effect if zones and zone ranges have
been defined for the empty collection.
To shard a collection using a compound hashed index, see 複合ハッシュされたインデックスを使用した初期チャンク分散.
複合ハッシュされたインデックスを使用した初期チャンク分散
MongoDB は、ハッシュされた複合インデックスでのコレクションのシャーディングをサポートしています。 複合ハッシュされたシャードキーを使用して、空のコレクションまたは存在しないコレクションをシャーディングする場合、MongoDB が初期チャンクの作成と分散を実行するために追加の要件が適用されます。
The numInitialChunks
option has no effect if zones and zone ranges
have been defined for the empty collection and
presplitHashedZones
is false
.
For an example, see 空、または存在しないコレクションのゾーンとゾーン範囲の事前定義.
以下も参照してください。
一意性
If you specify unique: true
, you must create the index
before using sh.shardAndDistributeCollection()
.
Although you can have a unique 複合インデックス where the shard
key is a prefix, if you use the unique
parameter, the collection must have a unique index that is on the shard
key.
以下も参照してください。
照合
If the collection has a default collation,
the sh.shardAndDistributeCollection
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.
例
The following examples show how you can use the
sh.shardAndDistributeCollection()
method with or without optional
parameters.
簡単な使い方
A database named records
contains a collection named people
. The
following command shards the collection by the zipcode
field and
immediately redistributes the data in the records.people
collection:
sh.shardAndDistributeCollection("records.people", { zipcode: 1 } )
オプションで使用
The phonebook
database has a contacts
collection with no
default collation. The
following example uses sh.shardAndDistributeCollection()
to shard and
redistribute the phonebook.contacts
collection with:
A ハッシュされたシャードキー on the
last_name
field.5
initial chunks.simple
collation.
sh.shardAndDistributeCollection( "phonebook.contacts", { last_name: "hashed" }, false, { numInitialChunks: 5, collation: { locale: "simple" } } )