Docs Menu
Docs Home
/ / /
C++ ドライバー
/

一括書き込み操作

項目一覧

  • Overview
  • サンプル データ
  • 一括書込み (write) インスタンスの作成
  • 書込み (write) 操作を定義する
  • 挿入操作
  • アップデート操作
  • 置換操作
  • 削除操作
  • 一括操作の実行
  • 一括書き込み操作をカスタマイズ
  • 戻り値
  • 詳細情報
  • API ドキュメント

このガイドでは、一括書込み操作を使用して、1 つのデータベース呼び出しで複数の書込み操作を実行する方法を学習できます。

コレクションにドキュメントを挿入 し、複数の他のドキュメントを更新してから、ドキュメントを削除するシナリオを考えてみましょう。 個々のメソッドを使用する場合、各操作には独自のデータベース呼び出しが必要です。 代わりに、 一括操作を使用して、データベースへの呼び出し回数を減らすことができます。

このガイドの例では、 Atlasサンプルデータセットsample_restaurantsデータベース内の restaurantsコレクションを使用します。 C++アプリケーションからこのコレクションにアクセスするには、Atlasmongocxx::client クラスターに接続する をインスタンス化し、 変数と 変数に次の値を割り当てます。dbcollection

auto db = client["sample_restaurants"];
auto collection = db["restaurants"];

MongoDB Atlasクラスターを無料で作成して、サンプルデータセットをロードする方法については、 「Atlas を使い始める」ガイドを参照してください。

一括書込み操作を実行中する前に、コレクションでcreate_bulk_write()メソッドを呼び出します。 このメソッドは、実行する一括書き込みのタイプに関する指示を保存するために使用できるmongocxx::bulk_writeクラスのインスタンスを返します。

次の例では、 restaurantsコレクションでcreate_bulk_write()メソッドを呼び出します。

auto bulk = collection.create_bulk_write();

次に、 mongocxx::bulk_writeインスタンスに書込みモデルを追加して、 一括操作を定義できます。 詳細については、次の「 書込み操作の定義 」セクションを参照してください。

実行する書込み操作ごとに、次のいずれかのモデル クラスのインスタンスを作成します。

  • mongocxx::model::insert_one

  • mongocxx::model::update_one

  • mongocxx::model::update_many

  • mongocxx::model::replace_one

  • mongocxx::model::delete_one

  • mongocxx::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インスタンスに追加された順序で操作を実行します。

次の例では、対応する各書込みモデルをmongocxx::bulk_writeのインスタンスに追加し、 execute()メソッドを呼び出すことで、このガイドの前述のセクションで指定されている挿入更新置換、および削除操作を実行します。 次に、変更されたドキュメントの数を表示します。

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インスタンスで設定できるフィールドを説明しています。

フィールド
説明
ordered
If true, the driver performs the write operations in the order provided. If an error occurs, the remaining operations are not attempted.

If false, the driver performs the operations in an arbitrary order and attempts to perform all operations.
Defaults to true.
bypass_document_validation
Specifies whether the operation bypasses document-level validation. For more information, see Schema Validation in the MongoDB Server manual.
Defaults to false.
write_concern
Specifies the write concern for the bulk operation. For more information, see Write Concern in the MongoDB Server manual.
comment
Attaches a comment to the operation. For more information, see the delete command fields guide in the MongoDB Server manual.
let
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()メソッドを呼び出しますが、 mongocxx::options::bulk_writeインスタンスのorderedフィールドを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クラスには、次のメンバー関数が含まれています。

関数
説明
deleted_count()
Returns the number of documents deleted, if any.
inserted_count()
Returns the number of documents inserted, if any.
matched_count()
Returns the number of documents matched for an update, if applicable.
modified_count()
Returns the number of documents modified, if any.
upserted_count()
Returns the number of documents upserted, if any.
upserted_ids()
Returns a map of the operation's index to the _id of the upserted documents, if applicable.

個々の書込み操作を実行する方法については、次のガイドを参照してください。

  • ドキュメントの挿入

  • Update Documents

  • Delete Documents

このガイドで説明したメソッドや型の詳細については、次の API ドキュメントを参照してください。

戻る

Delete Documents