对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

Update Documents

在本指南中,您可以学习如何使用C++驱动程序更新MongoDB集合中的文档。您可以调用 update_one() 方法更新单个文档,也可以调用 update_many() 方法更新多个文档。

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

要学习如何创建免费的MongoDB Atlas 集群并加载示例数据集,请参阅MongoDB 入门指南。

您可以使用以下方法在 MongoDB 中执行更新操作:

  • update_one(),更新匹配搜索条件的第一个文档

  • update_many(),更新与搜索条件匹配的所有文档

每种更新方法都需要以下参数:

  • 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.

  • 更新文档:指定更新操作符或要执行的更新类型,以及要更改的字段和值。 有关更新操作符及其用法的列表,请参阅MongoDB Server手册中的字段更新操作符指南。

以下示例使用update_one()方法将restaurants集合中某一文档的name值从"Bagels N Buns"更新为"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());

以下示例使用update_many()方法更新cuisine值为"Pizza"的所有文档。 更新后,文档的cuisine值为"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());

您可以通过将mongocxx::options::update类的实例作为可选参数传递来修改update_one()和update_many()方法的行为。 下表描述了您可以在mongocxx::options::update实例中设立的字段:

字段
说明

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

指定如果操作修改大量字段,则更新应用于哪些大量元素。

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.

以下示例使用 update_many() 方法查找 borough 值为 "Manhattan" 的所有文档。然后,它将这些文档中的 borough 值更新为 "Manhattan (north)"。由于 upsert 选项设立为 true,因此,如果查询筛选条件与任何现有文档都不匹配, C++驱动程序将插入一个新文档。

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);

update_one()和update_many()方法返回mongocxx::result::update类的实例。 该类包含以下成员函数:

function
说明

matched_count()

返回与查询过滤匹配的文档数,无论有多少文档已更新。

modified_count()

返回更新操作修改的文档数。 如果更新后的文档与原始文档相同,则不计入此计数。

result()

返回操作的原始结果文档。

upserted_count()

返回已更新或插入到数据库中的文档数。

upserted_id()

如果驱动程序执行了更新或插入(upsert),则返回在数据库中更新或插入的文档的ID。

以下示例使用update_many()方法将匹配文档的name字段从"Dunkin' Donuts"更新为"Dunkin'" 。 它调用modified_count()成员函数来打印已修改文档的数量:

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

要了解创建查询筛选器的更多信息,请参阅指定查询指南。

要进一步了解本指南所讨论的任何方法或类型,请参阅以下 API 文档: