Docs Menu
Docs Home
/ / /
C++ Driver
/

Delete Documents

On this page

  • Overview
  • Sample Data
  • Delete Operations
  • Delete One Document
  • Delete Multiple Documents
  • Customize the Delete Operation
  • Return Value
  • API Documentation

In this guide, you can learn how to use the C++ driver to remove documents from a MongoDB collection by performing delete operations.

A delete operation removes one or more documents from a MongoDB collection. You can perform a delete operation by using the delete_one() or delete_many() methods.

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"];

To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the Get Started with Atlas guide.

You can perform delete operations by using the following methods:

  • delete_one(), which deletes the first document that matches the search criteria

  • delete_many(), which deletes all documents that match the search criteria

Each delete method requires a query filter document, which specifies the search criteria to determine which documents to select for removal. For more information about query filters, see the Query Filter Documents section in the MongoDB Server manual.

The following example uses the delete_one() method to remove a document in the restaurants collection that has a name value of "Ready Penny Inn":

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

The following example uses the delete_many() method to remove all documents in the restaurants collection that have a borough value of "Brooklyn":

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

You can modify the behavior of the delete_one() and delete_many() methods by passing an instance of the mongocxx::options::delete_options class as an optional parameter. The following table describes the fields you can set in a mongocxx::options::delete_options instance:

Field
Description

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.

The following example calls the delete_many() method to delete all documents in the restaurants collection that have a name value containing the string "Mongo". It also sets the comment field of a mongocxx::options::delete_options instance to add a comment to the operation:

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

If the preceding example used the delete_one() method instead of delete_many(), the driver would delete only the first document that has a name value containing "Mongo".

The delete_one() and delete_many() methods return an instance of the mongocxx::result::delete_result class. This class contains the following member functions:

  • result(), which returns the raw bulk write result

  • deleted_count(), which returns the number of documents deleted

If the query filter does not match any documents, the driver doesn't delete any documents and deleted_count is 0.

The following example calls the delete_many() method to delete documents that have a cuisine value of "Greek". It then calls the deleted_count() member function to print the number of deleted documents:

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

To learn more about any of the methods or types discussed in this guide, see the following API documentation:

Back

Replace