sh.shardCollection()
定義
sh.shardCollection(namespace, key, unique, options)
Shards a collection using the
key
as a the シャードキー. The shard key determines how MongoDB distributes the collection's documents among the shards.注意
バージョン 6.0 での変更。
Starting in MongoDB 6.0, sharding a collection does ではない require you to first run the
sh.enableSharding()
method to configure the database.重要
mongosh メソッド
このページでは、
mongosh
メソッドについて記載しています。ただし、データベースコマンドや Node.js などの言語固有のドライバーのドキュメントには該当しません。データベースコマンドについては、
shardCollection
コマンドを参照してください。MongoDB API ドライバーについては、各言語の MongoDB ドライバー ドキュメントを参照してください。
sh.shardCollection()
takes the following arguments:Parameterタイプ説明namespace
string
"<database>.<collection>"
形式でシャーディングするコレクションの名前空間。
key
ドキュメント
シャードキー として使用するフィールドを指定するドキュメント。
{ <field1>: <1|"hashed">, ... }
Set the field value to either:
範囲ベースのシャーディングの
場合は
1
"hashed"
でハッシュされたシャードキーを指定します。
シャードキーはインデックスでサポートされている必要があります。コレクションが空でない限り、
shardCollection
コマンドの前にインデックスが存在している必要があります。コレクションが空の場合、シャードキーをサポートできるインデックスがまだ存在しない場合には、MongoDB はコレクションをシャーディングする前にインデックスを作成します。「シャードキー インデックス」も参照してください
unique
ブール値
Optional. Specify
true
to ensure that the underlying index enforces a unique constraint. Defaults tofalse
.ハッシュされたシャードキーを使用する場合、
true
は指定できません。For レガシー mongo shell, you must explicitly specify the value for
unique
if you specify theoptions
document.mongosh
doesn't requireunique
when you specify theoptions
document.options
ドキュメント
Optional. A document containing optional fields, including
numInitialChunks
andcollation
.The
options
argument supports the following options:Parameterタイプ説明numInitialChunks
integer
Optional. Specifies the minimum number of chunks to create initially when sharding an empty collection with a ハッシュされたシャードキー. MongoDB then creates and balances chunks across the cluster. The
numInitialChunks
parameter must be less than8192
chunks per shard. Defaults to2
chunks per shard.コレクションが空でない場合、またはシャードキーにハッシュされたフィールドが含まれていない場合、操作はエラーを返します。
presplitHashedZones: true でシャーディング場合、 MongoDB は指定された数のチャンクをクラスター内のゾーン全体に均等に分散しようとします。
presplitHashedZones: false または省略でシャーディング、空のコレクションにゾーンとゾーン範囲が定義されていない場合、 MongoDB は指定された数のチャンクをクラスター内のシャード全体に均等に分散しようとします。
presplitHashedZones: false または省略でシャーディング、空のコレクションに対してゾーンとゾーン範囲が定義されている場合、
numInitialChunks
は効果がありません。
collation
ドキュメント
Optional. If the collection specified to
shardCollection
has a default collation, you must include a collation document with``{ locale : "simple" }``, or theshardCollection
command fails. At least one of the indexes whose fields support the shard key pattern must have the simple collation.ブール値
任意。 コレクションに定義されたゾーンとゾーン範囲に基づいて、空または存在しないコレクションの初期チャンク作成と分散を実行するには、
true
を指定します。 ハッシュされたシャーディングのみ。次のいずれかに当てはまる場合、
shardCollection()
とpresplitHashedZones: true
はエラーを返します。シャードキーにはハッシュされたフィールドが含まれていません(単一フィールド ハッシュされたインデックスまたはハッシュされた複合インデックス ではない)。
コレクションには、定義されたゾーンまたはゾーン範囲はありません。
The defined zone ranges do not meet the requirements.
ドキュメント
任意。 新しいシャーディングされた時系列コレクションを作成するには、このオプションを指定します。
既存の時系列コレクションをシャーディングするには、このパラメーターを省略します。
shardCollection
に指定されたコレクションが時系列コレクションで、かつtimeseries
オプションが指定されていない場合、MongoDB は既存の時系列コレクションを定義する値を使用してtimeseries
フィールドに入力します。詳細な構文については「時系列オプション 」を参照してください。
時系列オプション
バージョン 5.1 で追加。
To create a new 時系列コレクション that is sharded, specify the
時系列 option to
sh.shardCollection()
.
The 時系列 option takes the following fields:
互換性
このメソッドは、次の環境でホストされている配置で使用できます。
MongoDB Atlas はクラウドでの MongoDB 配置のためのフルマネージド サービスです
重要
このコマンドは、M0、M2、M5、および Flex クラスターではサポートされていません。詳細については、「 サポートされていないコマンド 」を参照してください。
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
Considerations
シャードキー
シャードキーは後でも変更できますが、スケーラビリティやパフォーマンスの問題を回避するために、シャードキーの選択を慎重にすることが重要になります。
時系列コレクションのシャードキー
時系列コレクションをシャーディングする場合、シャードキーで指定できるのは次のフィールドのみです。
metaField
metaField
のサブフィールドtimeField
シャードキーでこれらのフィールドの組み合わせを指定できます。シャードキーのパターンでは、_id
を含む他のフィールドは許可されません。
シャードキーを指定する場合には、次のいずれかです。
metaField
は次のいずれかになります。timeField
は次の条件を満たす必要があります。シャードキーのパターン末尾
Tip
timeField
だけをシャードキーとして指定することは避けてください。
timeField
は単調に増加するため、すべての書き込みがクラスター内の 1 つのチャンクで発生することがあります。理想的には、データをチャンク間で均等に分散します。
シャードキーを最適に選択する方法については、以下を参照してください。
以下も参照してください。
ハッシュされたシャードキー
Hashed shard keys use a hashed index or a compound hashed index as the shard key.
ハッシュされたシャードキー フィールドを指定するには、 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.
例については、「 空または存在しないコレクションのゾーンとゾーン範囲の事前定義」を参照してください。 範囲指定または単一フィールドのハッシュされたシャードキーを使用してコレクションをシャーディングする場合、空のコレクションに対してゾーンとゾーン範囲が定義されていると、 numInitialChunks
オプションは効果がありません。
To shard a collection using a compound hashed index, see Initial Chunk Distribution with Compound Hashed Indexes.
Initial Chunk Distribution with Compound Hashed Indexes
MongoDB は、ハッシュされた複合インデックスでのコレクションのシャーディングをサポートしています。 複合ハッシュされたシャードキーを使用して、空のコレクションまたは存在しないコレクションをシャーディングする場合、MongoDB が初期チャンクの作成と分散を実行するために追加の要件が適用されます。
The numInitialChunks
option has no effect if zones and zone ranges
have been defined for the empty collection and
presplitHashedZones
is false
.
例については、「 空または存在しないコレクションのゾーンとゾーン範囲の事前定義」を参照してください。
以下も参照してください。
一意性
unique: true
を指定する場合
コレクションが空の場合、
sh.shardCollection()
はシャードキーに一意なインデックスがなければそれを作成します。コレクションが空でない場合、
sh.shardCollection()
を使用する前に、まずインデックスを作成する必要があります。
シャードキーがプレフィックスであるユニーク複合インデックスを持つことは可能ですが、unique
パラメーターを使用する場合、コレクションにはシャードキー上のユニークインデックスが必要です。
「シャーディングされたコレクションと一意のインデックス」も参照してください
照合
コレクションにデフォルトの照合がある場合、sh.shardCollection()
コマンドには { locale: "simple" }
の値を持つ collation
パラメーターが含まれている必要があります。デフォルトの照合を使用する空でないコレクションの場合、フィールドがシャードキー パターンをサポートする単純照合のインデックスが少なくとも 1 つ必要です。
照合のないコレクションにはcollation
オプションを指定する必要はありません。 照合のないコレクションに対して 照合オプションを指定しても、効果はありません。
書込み保証 (write concern)
mongos
uses "majority"
for the
書込み保証 (write concern) of the
shardCollection
command, its helper
sh.shardCollection()
, and the
sh.shardAndDistributeCollection()
method.
例
Simple Usage
Given a collection named people
in a database named records
,
the following command shards the collection by the
zipcode
field:
sh.shardCollection("records.people", { zipcode: 1 } )
Usage with Options
The phonebook
database has a collection contacts
with no
default collation. The
following example uses
sh.shardCollection()
to shard the phonebook.contacts
with:
a ハッシュされたシャードキー on the
last_name
field,5
initial chunks, anda collation of
simple
.
sh.shardCollection( "phonebook.contacts", { last_name: "hashed" }, false, { numInitialChunks: 5, collation: { locale: "simple" } } )
以下も参照してください。