Docs Menu

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() in mongosh has the same result as consecutively running the shardCollection and reshardCollection commands.

sh.shardAndDistributeCollection()は次のパラメータを取ります。

Parameter
タイプ
必要性
説明

namespace

文字列

必須

The namespace of the collection to shard in the format "<database>.<collection>".

key

ドキュメント

必須

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

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

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

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

unique

ブール値

任意

基礎のインデックスが一意の制約を強制するようにするには、true を指定します。デフォルトは false です。

When using hashed shard keys, you can't specify true.

options

ドキュメント

任意

A document containing optional fields, including numInitialChunks and collation.

options 引数は以下のオプションをサポートしています。

Parameter
タイプ
説明

numInitialChunks

整数

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 numInitialChunks parameter must result in less than 8192 per shard. Defaults to 1000 chunks per shard.

collation

ドキュメント

If the collection specified to shardAndDistributeCollection() has a default collation, you must include a collation document with { locale : "simple" }, or the shardAndDistributeCollection() method fails.

presplitHashedZones

ブール値

Specify true to perform initial chunk creation and distribution for an empty or non-existing collection based on the defined zones and zone ranges for the collection. For hashed sharding only.

shardAndDistributeCollection() with presplitHashedZones: true returns an error if any of the following are true:

timeseries

ドキュメント

Specify this option to create a new sharded time series collection.

既存の時系列コレクションをシャーディングするには、このパラメーターを省略します。

When the collection specified to shardAndDistributeCollection is a time series collection and the timeseries option is not specified, MongoDB uses the values that define the existing time series collection to populate the timeseries field.

詳細な構文については「時系列オプション 」を参照してください。

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 を含む他のフィールドは許可されません。

シャードキーを指定する場合には、次のいずれかです。

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オプションを指定する必要はありません。 照合のないコレクションに対して 照合オプションを指定しても、効果はありません。

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:

sh.shardAndDistributeCollection(
"phonebook.contacts",
{ last_name: "hashed" },
false,
{
numInitialChunks: 5,
collation: { locale: "simple" }
}
)