Bulk()
Tip
MongoDB は、一括書き込み操作を実行するための db.collection.bulkWrite()
メソッドも提供します。
説明
Bulk()
Bulk operations builder used to construct a list of write operations to perform in bulk for a single collection. To instantiate the builder, use either the
db.collection.initializeOrderedBulkOp()
or thedb.collection.initializeUnorderedBulkOp()
method.
互換性
このコマンドは、次の環境でホストされている配置で使用できます。
MongoDB Atlas はクラウドでの MongoDB 配置のためのフルマネージド サービスです
注意
このコマンドは、すべての MongoDB Atlas クラスターでサポートされています。すべてのコマンドに対する Atlas のサポートについては、「サポートされていないコマンド」を参照してください。
Ordered and Unordered Bulk Operations
The builder can construct the list of operations as ordered or unordered.
順序付き操作
With an ordered operations list, MongoDB executes the write operations in the list serially. If an error occurs during the processing of one of the write operations, MongoDB will return without processing any remaining write operations in the list.
使用 db.collection.initializeOrderedBulkOp()
to create a
builder for an ordered list of write commands.
ordered
の操作リストを実行するとき、MongoDB は operation type
と連続性によって操作をグループ化します。つまり、同じタイプの連続した操作がグループ化されます。たとえば、順序付きリストに 2 回の挿入操作があり、その後に更新操作、さらに別の挿入操作が続く場合、MongoDB は操作を 3 つのグループに分けます。最初のグループには 2 回の挿入操作、2 番目のグループには更新操作、3 番目のグループには最後の挿入操作が含まれます。この動作は、将来のバージョンで変更される可能性があります。
Bulk()
operations in mongosh
and comparable
methods in the drivers do not have a limit for the number of operations
in a group. To see how the operations are grouped for bulk operation
execution, call Bulk.getOperations()
after the execution.
以下も参照してください。
シャーディングされたコレクションで ordered
操作リストを実行すると、通常、 unordered
リストの実行よりも時間がかかります。これは、ordered リストでは毎回の操作で前の操作の完了を待機する必要があるためです。
順序なし操作
With an unordered operations list, MongoDB can execute in parallel, as well as in a nondeterministic order, the write operations in the list. If an error occurs during the processing of one of the write operations, MongoDB will continue to process remaining write operations in the list.
使用 db.collection.initializeUnorderedBulkOp()
to create a
builder for an unordered list of write commands.
unordered
の操作リストを実行するとき、MongoDB は操作をグループ化します。順序なしの一括操作では、リスト内の操作の順序を変更してパフォーマンスを向上させることができます。そのため、アプリケーションは、unordered
一括操作を実行するときに順序に依存すべきではありません。
Bulk()
operations in mongosh
and comparable
methods in the drivers do not have a limit for the number of operations
in a group. To see how the operations are grouped for bulk operation
execution, call Bulk.getOperations()
after the execution.
以下も参照してください。
トランザクション
Bulk()
は分散トランザクション内で使用できます。
Bulk.insert()
操作の場合、コレクションがすでに存在している必要があります。
Bulk.find.upsert()
の場合、操作の結果がアップサートになる場合は、コレクションがすでに存在している必要があります。
トランザクションで実行される場合、操作の書込み保証 (write concern)を明示的に設定しないでください。トランザクションで書込み保証を使用するには、「トランザクション書込み保証」を参照してください。
重要
ほとんどの場合、分散トランザクションでは 1 つのドキュメントの書き込み (write) よりもパフォーマンス コストが高くなります。分散トランザクションの可用性は、効果的なスキーマ設計の代わりにはなりません。多くのシナリオにおいて、非正規化されたデータモデル(埋め込みドキュメントと配列)が引き続きデータやユースケースに最適です。つまり、多くのシナリオにおいて、データを適切にモデリングすることで、分散トランザクションの必要性を最小限に抑えることができます。
トランザクションの使用に関するその他の考慮事項(ランタイム制限や oplog サイズ制限など)については、「本番環境での考慮事項」も参照してください。
メソッド
The Bulk()
builder has the following methods:
名前 | 説明 |
---|---|
挿入オペレーションを操作リストに追加します。 | |
アップデート操作または削除操作のクエリ条件を指定します。 | |
複数のドキュメントの削除操作を操作リストに追加します。 | |
1 つのドキュメントの削除操作を操作リストに追加します。 | |
| |
| |
1 つのドキュメントの置換操作を操作リストに追加します。 | |
1 つのドキュメントのアップデート操作を操作リストに追加します。 | |
| |
アップデート操作の | |
操作リストを一括で実行します。 | |
| |
| |
|