Tip
MongoDB は、一括書き込み操作を実行するための db.collection.bulkWrite() メソッドも提供します。
定義
db.collection.initializeOrderedBulkOp()重要
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) 操作の順序付きリストを作成します。次の値を返します。 新しい
Bulk()操作ビルダ オブジェクト。
互換性
このコマンドは、次の環境でホストされている配置で使用できます。
- MongoDB Atlas はクラウドでの MongoDB 配置のための完全管理サービスです
注意
このコマンドは、すべての MongoDB Atlas クラスターでサポートされています。すべてのコマンドに対する Atlas のサポートについては、「サポートされていないコマンド」を参照してください。
動作
操作の順序
順序付きの操作リストの場合、MongoDB はリスト内の書込み操作を順番に実行します。
操作の実行
When executing an ordered list of operations, MongoDB groups the operations by the operation type and contiguity; i.e. contiguous operations of the same type are grouped together. For example, if an ordered list has two insert operations followed by an update operation followed by another insert operation, MongoDB groups the operations into three separate groups: first group contains the two insert operations, second group contains the update operation, and the third group contains the last insert operation. This behavior is subject to change in future versions.
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.
Executing an ordered list of operations on a sharded collection will generally be slower than executing an unordered list since with an ordered list, each operation must wait for the previous operation to finish.
Error Handling
いずれかの書込み操作の処理中にエラーが発生した場合、MongoDB はリスト内の残りの書込み操作を処理せずに返します。
例
以下の例では、 usersコレクションでBulk()操作ビルダを初期化し、一連の書込み操作を追加して操作を実行します。
var bulk = db.users.initializeOrderedBulkOp(); 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.find( { status: "D" } ).delete(); bulk.find( { status: "P" } ).update( { $set: { comment: "Pending" } } ); bulk.execute();