Docs 菜单
Docs 主页
/ / /
C++ 驱动程序
/

Update Documents

在此页面上

  • Overview
  • 样本数据
  • 更新操作
  • 更新一个文档
  • 更新多个文档
  • 自定义更新操作
  • 返回值
  • 更多信息
  • API 文档

在本指南中,您可以学习;了解如何使用C++驾驶员更新MongoDB集合中的文档。 您可以调用 update_one()方法更新单个文档,也可以调用update_many()方法更新多个文档。

本指南中的示例使用 Atlas示例数据集sample_restaurants数据库中的restaurants集合。 要从C++应用程序访问权限此集合,请实例化一个连接到Atlas 集群的mongocxx::client ,并将以下值分配给dbcollection变量:

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

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

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

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

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

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

  • 查询过滤文档:指定要更新的文档。 有关查询筛选器的更多信息,请参阅MongoDB Server手册中的查询筛选器文档部分

  • 更新文档:指定更新操作符或要执行的更新类型,以及要更改的字段和值。 有关更新操作符及其用法的列表,请参阅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
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.

以下示例使用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()
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.

以下示例使用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 文档:

后退

插入文档