Tip
MongoDB は、一括書き込み操作を実行するための db.collection.bulkWrite() メソッドも提供します。
定義
db.collection.initializeUnorderedBulkOp()重要
mongosh メソッド
This page documents a
mongoshmethod. This is not the documentation for a language-specific driver, such as Node.js.MongoDB API ドライバーについては、各言語の「MongoDB ドライバーのドキュメント」を参照してください。
コレクションの新しい
Bulk()操作ビルダを初期化して返します。 ビルダは、MongoDB が一括して実行する書込み (write) 操作の順序なしのリストを作成します。
互換性
このコマンドは、次の環境でホストされている配置で使用できます。
- MongoDB Atlas はクラウドでの MongoDB 配置のための完全管理サービスです
注意
このコマンドは、すべての MongoDB Atlas クラスターでサポートされています。すべてのコマンドに対する Atlas のサポートについては、「サポートされていないコマンド」を参照してください。
動作
操作の順序
順序なしの操作リストを使用すると、MongoDB はリスト内の書込み操作を並行して実行でき、任意の順序で実行できます。 操作の順序が重要な場合は、代わりにdb.collection.initializeOrderedBulkOp()を使用します。
操作の実行
When executing an unordered list of operations, MongoDB groups the operations. With an unordered bulk operation, the operations in the list may be reordered to increase performance. As such, applications should not depend on the ordering when performing unordered bulk operations.
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.
Error Handling
いずれかの書込み操作の処理中にエラーが発生した場合、MongoDB はリスト内の残りの書込み操作の処理を続行します。
例
以下の例では、 Bulk()操作ビルダーを初期化し、複数のドキュメントを追加するための一連の挿入操作を追加します。
var bulk = db.users.initializeUnorderedBulkOp(); bulk.insert( { user: "abc123", status: "A", points: 0 } ); bulk.insert( { user: "ijk123", status: "A", points: 0 } ); bulk.insert( { user: "mop123", status: "P", points: 0 } ); bulk.execute();