Overview
このガイドでは、一括書込み操作を使用して、1 つのデータベース呼び出しで複数の書込み操作を実行する方法を学習できます。
コレクションにドキュメントを挿入 し、複数の他のドキュメントを更新してから、ドキュメントを削除するシナリオを考えてみましょう。 個々のメソッドを使用する場合、各操作には独自のデータベース呼び出しが必要です。 代わりに、 一括操作を使用して、データベースへの呼び出し回数を減らすことができます。
サンプル データ
The examples in this guide use the restaurants collection in the sample_restaurants database from the Atlas sample datasets. To access this collection from your C++ application, instantiate a mongocxx::client that connects to an Atlas cluster and assign the following values to your db and collection variables:
auto db = client["sample_restaurants"]; auto collection = db["restaurants"];
MongoDB Atlasクラスターを無料で作成して、サンプルデータセットをロードする方法については、MongoDBを使い始めるガイドを参照してください。
一括書込み (write) インスタンスの作成
一括書込み操作を実行中する前に、コレクションでcreate_bulk_write()メソッドを呼び出します。 このメソッドは、実行する一括書き込みのタイプに関する指示を保存するために使用できるmongocxx::bulk_writeクラスのインスタンスを返します。
次の例では、 restaurantsコレクションでcreate_bulk_write()メソッドを呼び出します。
auto bulk = collection.create_bulk_write();
次に、 mongocxx::bulk_writeインスタンスに書込みモデルを追加して、 一括操作を定義できます。 詳細については、次の「 書込み操作の定義 」セクションを参照してください。
書込み (write) 操作を定義する
実行する書込み操作ごとに、次のいずれかのモデル クラスのインスタンスを作成します。
mongocxx::model::insert_onemongocxx::model::update_onemongocxx::model::update_manymongocxx::model::replace_onemongocxx::model::delete_onemongocxx::model::delete_many
次に、 create_bulk_write()メソッドによって返されたmongocxx::bulk_writeインスタンスに各書き込みモデルを追加します。
次のセクションでは、前述の書込みモデル クラスのインスタンスを作成して使用する方法を示します。
挿入操作
挿入操作を実行するには、 mongocxx::model::insert_oneクラスのインスタンスを作成し、挿入するドキュメントを指定します。 次に、モデルインスタンスをmongocxx::bulk_writeクラスのインスタンスに追加します。
次の例では、 mongocxx::model::insert_oneのインスタンスを作成し、それをbulkと呼ばれるmongocxx::bulk_writeインスタンスに追加します。
auto insert_doc = make_document(kvp("name", "Mongo's Deli"), kvp("cuisine", "Sandwiches"), kvp("borough", "Manhattan"), kvp("restaurant_id", "1234")); mongocxx::model::insert_one insert_op{insert_doc.view()}; bulk.append(insert_op);
複数のドキュメントを挿入するには、ドキュメントごとにmongocxx::model::insert_oneのインスタンスを作成します。
アップデート操作
ドキュメントを更新するには、 mongocxx::model::update_oneのインスタンスを作成します。 このモデルは、クエリフィルターに一致する最初のドキュメントを更新するようにドライバーに指示します。 次に、モデルインスタンスをmongocxx::bulk_writeクラスのインスタンスに追加します。
次の引数をmongocxx::model::update_oneモデルに渡します。
クエリフィルタードキュメントは、コレクション内のドキュメントをマッチングするために使用される基準を指定します。
ドキュメントを更新し、実行する更新の種類を指定します。 更新操作の詳細については、 MongoDB Serverマニュアルの「フィールド更新演算子」ガイドを参照してください。
次の例では、 mongocxx::model::update_oneのインスタンスを作成し、それをbulkと呼ばれるmongocxx::bulk_writeインスタンスに追加します。
auto filter_doc = make_document(kvp("name", "Mongo's Deli")); auto update_doc = make_document(kvp("$set", make_document(kvp("cuisine", "Sandwiches and Salads")))); mongocxx::model::update_one update_op{filter_doc.view(), update_doc.view()}; bulk.append(update_op);
複数のドキュメントを更新するには、 mongocxx::model::update_manyのインスタンスを作成し、同じ引数で渡します。 このモデルは、クエリフィルターに一致するすべてのドキュメントを更新するようにドライバーに指示します。
次の例では、 mongocxx::model::update_manyのインスタンスを作成し、それをbulkに追加します。
auto filter_doc = make_document(kvp("name", "Mongo's Deli")); auto update_doc = make_document(kvp("$set", make_document(kvp("cuisine", "Sandwiches and Salads")))); mongocxx::model::update_many update_op{filter_doc.view(), update_doc.view()}; bulk.append(update_op);
置換操作
置換操作により、指定されたドキュメントのすべてのフィールドと値が削除され、新しいフィールドと値に置き換えられます。 置換操作を実行するには、 mongocxx::model::replace_oneクラスのインスタンスを作成し、そのインスタンスにクエリフィルターと 一致するドキュメントに保存するフィールドと値を渡します。 次に、モデルインスタンスをmongocxx::bulk_writeクラスのインスタンスに追加します。
次の例では、 mongocxx::model::replace_oneのインスタンスを作成し、それをbulkと呼ばれるmongocxx::bulk_writeインスタンスに追加します。
auto filter_doc = make_document(kvp("restaurant_id", "1234")); auto replace_doc = make_document(kvp("name", "Mongo's Deli"), kvp("cuisine", "Sandwiches and Salads"), kvp("borough", "Brooklyn"), kvp("restaurant_id", "5678")); mongocxx::model::replace_one replace_op{filter_doc.view(), replace_doc.view()}; bulk.append(replace_op);
複数のドキュメントを置き換えるには、ドキュメントごとにmongocxx::model::replace_oneの新しいインスタンスを作成する必要があります。
削除操作
ドキュメントを削除するには、 mongocxx::model::delete_oneクラスのインスタンスを作成し、削除するドキュメントを指定するクエリフィルターを渡します。 このモデルは、クエリフィルターに一致する最初のドキュメントのみを削除するようにドライバーに指示します。 次に、モデルインスタンスをmongocxx::bulk_writeクラスのインスタンスに追加します。
次の例では、 mongocxx::model::delete_oneのインスタンスを作成し、それをbulkと呼ばれるmongocxx::bulk_writeインスタンスに追加します。
auto filter_doc = make_document(kvp("restaurant_id", "5678")); mongocxx::model::delete_one delete_op{filter_doc.view()}; bulk.append(delete_op);
複数のドキュメントを削除するには、 mongocxx::model::delete_manyクラスのインスタンスを作成し、削除するドキュメントを指定するクエリフィルターで渡します。 このモデルは、クエリフィルターに一致するすべてのドキュメントを削除するようにドライバーに指示します。
次の例では、 mongocxx::model::delete_manyのインスタンスを作成し、それをbulkに追加します。
auto filter_doc = make_document(kvp("borough", "Manhattan")); mongocxx::model::delete_many delete_op{filter_doc.view()}; bulk.append(delete_op);
一括操作の実行
一括操作を実行するには、書込みモデルを含むmongocxx::bulk_writeクラスのインスタンスでexecute()メソッドを呼び出します。 デフォルトでは 、 execute()メソッドはmongocxx::bulk_writeインスタンスに追加された順序で操作を実行します。
The following example performs the insert, update, replace, and delete operations specified in the preceding sections of this guide by appending each corresponding write model to an instance of mongocxx::bulk_write and calling the execute() method. Then, it prints the number of modified documents:
auto bulk = collection.create_bulk_write(); // Specifies documents to insert, update, replace, or delete auto insert_doc = make_document(kvp("name", "Mongo's Deli"), kvp("cuisine", "Sandwiches"), kvp("borough", "Manhattan"), kvp("restaurant_id", "1234")); auto update_filter = make_document(kvp("name", "Mongo's Deli")); auto update_doc = make_document(kvp("$set", make_document(kvp("cuisine", "Sandwiches and Salads")))); auto replace_filter = make_document(kvp("restaurant_id", "1234")); auto replace_doc = make_document(kvp("name", "Mongo's Deli"), kvp("cuisine", "Sandwiches and Salads"), kvp("borough", "Brooklyn"), kvp("restaurant_id", "5678")); auto delete_filter = make_document(kvp("borough", "Manhattan")); // Creates write models for each write operation using the preceding documents mongocxx::model::insert_one insert_op{insert_doc.view()}; mongocxx::model::update_many update_op{update_filter.view(), update_doc.view()}; mongocxx::model::replace_one replace_op{replace_filter.view(), replace_doc.view()}; mongocxx::model::delete_many delete_op{delete_filter.view()}; // Appends each write model to the bulk operation bulk.append(insert_op); bulk.append(update_op); bulk.append(replace_op); bulk.append(delete_op); // Runs the bulk operation auto result = bulk.execute(); std::cout << "Modified documents: " << result->modified_count() << std::endl;
Modified documents: 2
書き込み操作のいずれかが失敗した場合、 C++ドライバーはmongocxx::bulk_write_exceptionを発生させ、それ以上の操作を実行しません。
Tip
modified_count()関数の詳細については、このガイドの「戻り値 」セクションを参照してください。
一括書き込み操作をカスタマイズ
mongocxx::options::bulk_writeクラスのインスタンスをパラメーターとして渡すことで、 create_bulk_write()メソッドの動作を変更できます。 次の表では、 mongocxx::options::bulk_writeインスタンスで設定できるフィールドを説明しています。
フィールド | 説明 |
|---|---|
|
|
| 操作がドキュメントレベルの検証をバイパスするかどうかを指定します。詳細については、 MongoDB Serverマニュアルの「 スキーマバリデーション 」を参照してください。デフォルトは |
| 一括操作の書込み保証 (write concern)を指定します。詳細については、 MongoDB Serverマニュアルの「 書込み保証 」を参照してください。 |
| Attaches a comment to the operation. For more information, see the delete command fields guide in the MongoDB Server manual. |
| Specifies a document with a list of values to improve operation readability. Values must be constant or closed expressions that don't reference document fields. For more information, see the let statement in the MongoDB Server manual. |
次の例では、このページのcreate_bulk_write() 一括書込みインスタンスの作成 の例から メソッドを呼び出しますが、ordered インスタンスのmongocxx::options::bulk_write フィールドをfalse に設定します。
mongocxx::options::bulk_write opts; opts.ordered(false); auto bulk = collection.create_bulk_write(opts);
順序付けなし一括書き込み 内のいずれかの書込み操作が失敗した場合、 C++ドライバーはすべての操作を試行した後にのみエラーを報告します。
注意
順序なしの一括操作では、実行順序は保証されません。 この順序は、ランタイムを最適化するために一覧表示する方法とは異なる場合があります。
戻り値
execute()メソッドはmongocxx::result::bulk_writeクラスのインスタンスを返します。 mongocxx::result::bulk_writeクラスには、次のメンバー関数が含まれています。
関数 | 説明 |
|---|---|
| 削除されたドキュメントの数を返します(存在する場合)。 |
| 挿入されたドキュメントの数を返します(存在する場合)。 |
| 該当する場合は、アップデートに一致したドキュメントの数を返します。 |
| 変更されたドキュメントの数を返します(存在する場合)。 |
| アップサートされたドキュメントの数を返します(存在する場合)。 |
| アップサートされたドキュメントの |
詳細情報
個々の書込み操作を実行する方法については、次のガイドを参照してください。
API ドキュメント
このガイドで説明したメソッドや型の詳細については、次の API ドキュメントを参照してください。