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

Delete Documents

項目一覧

  • Overview
  • サンプル データ
  • 削除操作
  • 単一ドキュメントの削除
  • 複数のドキュメントの削除
  • 削除操作をカスタマイズする
  • 戻り値
  • API ドキュメント

このガイドでは、 C++ドライバーを使用して、削除操作を実行し、 MongoDBコレクションからドキュメントを削除する方法を学習できます。

削除操作は、MongoDB コレクションから 1 つ以上のドキュメントを削除します。 削除操作は、 delete_one()またはdelete_many()メソッドを使用して実行できます。

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

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

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

次の方法を使用して削除操作を実行できます。

  • delete_one()は、検索条件に一致する最初のドキュメントを削除します。

  • delete_many()は、検索条件に一致するすべてのドキュメントを削除します

各削除メソッドにはクエリフィルタードキュメントが必要です。このドキュメントは、削除対象として選択するドキュメントを決定するための検索条件を指定します。 クエリフィルターの詳細については、 MongoDB Serverマニュアルの「クエリフィルター ドキュメント 」セクションを参照してください。

次の例では、 delete_one()メソッドを使用して、 nameの値が"Ready Penny Inn"であるrestaurantsコレクション内のドキュメントを削除します。

auto result = collection.delete_one(make_document(kvp("name", "Ready Penny Inn")));

次の例では、 delete_many()メソッドを使用して、 boroughの値が"Brooklyn"であるrestaurantsコレクション内のすべてのドキュメントを削除します。

auto result = collection.delete_many(make_document(kvp("borough", "Brooklyn")));

mongocxx::options::delete_optionsクラスのインスタンスを任意のパラメータとして渡すことで、 delete_one()メソッドとdelete_many()メソッドの動作を変更できます。 次の表では、 mongocxx::options::delete_optionsインスタンスに設定できるフィールドを説明しています。

フィールド
説明
collation
Specifies the kind of language collation to use when sorting results. For more information, see Collation in the MongoDB Server manual.
write_concern
Sets the write concern for the operation. For more information, see Write Concern in the MongoDB Server manual.
hint
Gets or sets the index to scan for documents. For more information, see the hint statement 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.
comment
Attaches a comment to the operation. For more information, see the delete command fields guide in the MongoDB Server manual.

次の例では、 delete_many()メソッドを呼び出して、 name値に string "Mongo"が含まれるrestaurantsコレクション内のすべてのドキュメントを削除します。 また、 mongocxx::options::delete_optionsインスタンスのcommentフィールドを設定して、操作にコメントを追加します。

mongocxx::options::delete_options opts{};
opts.comment(bsoncxx::types::bson_value::view_or_value{"Deleting Mongo restaurants"});
auto query_filter = make_document(kvp("name", make_document(kvp("$regex", "Mongo"))));
auto result = collection.delete_many(query_filter.view(), opts);

Tip

上記の例でdelete_many()ではなくdelete_one()メソッドが使用されている場合、ドライバーはname値に"Mongo"を含む最初のドキュメントのみを削除します。

delete_one()メソッドとdelete_many()メソッドはmongocxx::result::delete_resultクラスのインスタンスを返します。 このクラスには、次のメンバー関数が含まれています。

  • result()は、未加工の一括書込み結果を返します

  • deleted_count()は、削除されたドキュメントの数を返します

クエリフィルターがどのドキュメントにも一致しない場合、ドライバーはドキュメントを削除せず、 deleted_countは0です。

次の例では、 delete_many()メソッドを呼び出して、 cuisineの値が"Greek"であるドキュメントを削除します。 次に、 deleted_count()ノード関数を呼び出して、削除されたドキュメント数を出力します。

auto result = collection.delete_many(make_document(kvp("cuisine", "Greek")));
std::cout << result->deleted_count() << std::endl;
Deleted documents: 111

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

戻る

Update Documents