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

Update Documents

On this page

  • Overview
  • Sample Data
  • Update Operations
  • Update One Document
  • Update Many Documents
  • Customize the Update Operation
  • Return Value
  • Additional Information
  • API Documentation

In this guide, you can learn how to use the C++ driver to update documents in a MongoDB collection. You can call the update_one() method to update a single document or the update_many() method to update multiple documents.

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 update operations in MongoDB by using the following methods:

  • update_one(), which updates the first document that matches the search criteria

  • update_many(), which updates all documents that match the search criteria

Each update method requires the following parameters:

  • Query filter document: Specifies which documents to update. For more information about query filters, see the Query Filter Documents section in the MongoDB Server manual.

  • Update document: Specifies the update operator, or the kind of update to perform, and the fields and values to change. For a list of update operators and their usage, see the Field Update Operators guide in the MongoDB Server manual.

The following example uses the update_one() method to update the name value of a document in the restaurants collection from "Bagels N Buns" to "2 Bagels 2 Buns":

auto query_filter = make_document(kvp("name", "Bagels N Buns"));
auto update_doc = make_document(kvp("$set", make_document(kvp("name", "2 Bagels 2 Buns"))));
auto result = collection.update_one(query_filter.view(), update_doc.view());

The following example uses the update_many() method to update all documents that have a cuisine value of "Pizza". After the update, the documents have a cuisine value of "Pasta".

auto query_filter = make_document(kvp("cuisine", "Pizza"));
auto update_doc = make_document(kvp("$set", make_document(kvp("cuisine", "Pasta"))));
auto result = collection.update_many(query_filter.view(), update_doc.view());

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

Field
Description
upsert
Specifies whether the update operation performs an upsert operation if no documents match the query filter. For more information, see the upsert statement in the MongoDB Server manual.
Defaults to false.
bypass_document_validation
Specifies whether the update operation bypasses document validation. This lets you update documents that don't meet the schema validation requirements, if any exist. For more information about schema validation, see Schema Validation in the MongoDB Server manual.
Defaults to false.
collation
Specifies the kind of language collation to use when sorting results. For more information, see Collation in the MongoDB Server manual.
array_filters
Specifies which array elements an update applies to if the operation modifies array fields.
hint
Sets the index to scan for documents. For more information, see the hint statement 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.
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
A comment to attach to the operation. For more information, see the insert command fields guide in the MongoDB Server manual for more information.

The following example uses the update_many() method to find all documents that have borough value of "Manhattan". It then updates the borough value in these documents to "Manhattan (north)". Because the upsert option is set to true, the C++ driver inserts a new document if the query filter doesn't match any existing documents.

mongocxx::options::update opts{};
opts.upsert(true);
auto query_filter = make_document(kvp("borough", "Manhattan"));
auto update_doc = make_document(kvp("$set", make_document(kvp("borough", "Manhattan (north)"))));
auto result = collection.update_many(query_filter.view(), update_doc.view(), opts);

The update_one() and update_many() methods return an instance of the mongocxx::result::update class. This class contains the following member functions:

Function
Description
matched_count()
Returns the number of documents that matched the query filter, regardless of how many were updated.
modified_count()
Returns number of documents modified by the update operation. If an updated document is identical to the original, it is not included in this count.
result()
Returns the raw result document for the operation.
upserted_count()
Returns the number of document that were upserted into the database.
upserted_id()
Returns the ID of the document that was upserted in the database, if the driver performed an upsert.

The following example uses the update_many() method to update the name field of matching documents from "Dunkin' Donuts" to "Dunkin'". It calls the modified_count() member function to print the number of modified documents:

auto query_filter = make_document(kvp("name", "Dunkin' Donuts"));
auto update_doc = make_document(kvp("$set", make_document(kvp("name", "Dunkin'"))));
auto result = collection.update_many(query_filter.view(), update_doc.view());
std::cout << "Modified documents: " << result->modified_count() << std::endl;
Modified documents: 206

To learn more about creating query filters, see the Specify a Query guide.

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

Back

Insert