Update Documents

For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

Update Documents

Overview

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

Sample Data

The examples in this guide use the restaurants collection in the sample_restaurants database from the Atlas sample datasets. To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the MongoDB Get Started guide.

Update Operations

You can perform update operations in MongoDB by using the following functions:

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

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

Each update function accepts the following parameters:

  • Collection: Specifies the collection to update.

  • Query filter document: Specifies which collection documents to update. For more information about query filters, see the [Query Filter Documents section](https://www.mongodb.com/docs/manual/core/document/#query-filter-documents 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.

  • Options document: Specifies options to customize the operation, or NULL.

  • Results location: Specifies a pointer to overwritable storage that will contain operation results, or NULL.

  • Error location: Specifies a location for an error value, or NULL.

Update One Document

Use the following syntax to update a document:

bson_t *query = BCON_NEW("<field to match>", BCON_UTF8("<value to match>"));
bson_t *update = BCON_NEW("$set", "{", "<field name>", BCON_UTF8("<value>"), "}");
bson_error_t error;

if (!mongoc_collection_update_one(collection, query, update, NULL, NULL, &error)) {
    fprintf(stderr, "Update one operation failed: %s\n", error.message);
}

bson_destroy(query);
bson_destroy(update);

The following example uses the mongoc_collection_update_one() function to update the name value of a document in the restaurants collection from “Bagels N Buns” to “2 Bagels 2 Buns”:

bson_t *query = BCON_NEW("name", BCON_UTF8("Bagels N Buns"));
bson_t *update = BCON_NEW("$set", "{", "name", BCON_UTF8("2 Bagels 2 Buns"), "}");
bson_error_t error;

if (!mongoc_collection_update_one(collection, query, update, NULL, NULL, &error)) {
    fprintf(stderr, "Update one operation failed: %s\n", error.message);
}

bson_destroy(query);
bson_destroy(update);

Update Many Documents

Use the following syntax to update several documents:

bson_t *query = BCON_NEW("<field to match>", BCON_UTF8("<value to match>"));
bson_t *update = BCON_NEW("$set", "{", "<field name>", BCON_UTF8("<value>"), "}");
bson_error_t error;

if (!mongoc_collection_update_many(collection, query, update, NULL, NULL, &error)) {
    fprintf(stderr, "Update many operation failed: %s\n", error.message);
}

bson_destroy(query);
bson_destroy(update);

The following example uses the mongoc_collection_update_many() function to update all documents that have a cuisine value of “Pizza”. After the update, the documents have a cuisine value of “Pasta”.

bson_t *query = BCON_NEW("cuisine", BCON_UTF8("Pizza"));
bson_t *update = BCON_NEW("$set", "{", "cuisine", BCON_UTF8("Pasta"), "}");
bson_error_t error;

if (!mongoc_collection_update_many(collection, query, update, NULL, NULL, &error)) {
    fprintf(stderr, "Update many operation failed: %s\n", error.message);
}

bson_destroy(query);
bson_destroy(update);

Customize the Update Operation

You can modify the behavior of the mongoc_collection_update_one() and mongoc_collection_update_many() functions by passing a BSON document that specifies option values. The following table describes some options you can set in the document:

Option Description
bypassDocumentValidation If set to true, allows the write operation to opt out of document-level validation.Defaults to false.Type: bool
writeConcern Sets the write concern for the operation.Defaults to the write concern of the namespace.Type: mongoc_write_concern_t
collation Specifies the kind of language collation to use when comparing text. For more information, see [Collation](https://www.mongodb.com/docs/manual/reference/collation/#std-label-collation in the MongoDB Server manual.Type: bson_t
comment A comment to attach to the operation. For more information, see the [insert command fields](https://www.mongodb.com/docs/manual/reference/command/insert/#command-fields guide in the MongoDB Server manual.Type: bson_value_t
upsert A comment to attach to the operation. For more information, see the [insert command fields](https://www.mongodb.com/docs/manual/reference/command/insert/#command-fields guide in the MongoDB Server manual.Type: bson_value_t

Upsert a Document

The following example uses the mongoc_collection_update_many() function 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.

bson_t *query = BCON_NEW("borough", BCON_UTF8("Manhattan"));
bson_t *update = BCON_NEW("$set", "{", "borough", BCON_UTF8("Manhattan (north)"), "}");
bson_error_t error;
bson_t opts;

bson_init(&opts);
bson_append_bool(&opts, "upsert", -1, true);

if (!mongoc_collection_update_many(collection, query, update, &opts, NULL, &error)) {
    fprintf(stderr, "Update many operation failed: %s\n", error.message);
}

bson_destroy(query);
bson_destroy(update);
bson_destroy(&opts);

Additional Information

To learn more about creating query filters, see the [Specify a Query](https://www.mongodb.com/docs/languages/c/c-driver/current/crud/query-operations/specify-a-query.md#std-label-c-specify-query guide.

API Documentation

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