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

通过聚合转换数据

在本指南中,您可以了解如何使用 C++ 驱动程序来执行聚合操作。

聚合操作处理 MongoDB 集合中的数据并返回计算结果。 MongoDB 聚合框架是 Query API 的一部分,以数据处理管道的概念为模型。 文档进入包含一个或多个阶段的管道,该管道将文档转换为聚合结果。

提示

完成聚合教程

You can find tutorials that provide detailed explanations of common aggregation tasks in the Complete Aggregation Pipeline Tutorials section of the Server manual. Select a tutorial, and then pick C++11 from the Select your language drop-down menu in the upper-right corner of the page.

聚合操作类似于汽车工厂。汽车工厂有一条装配线,其中包含配备专用工具的装配站,用于完成特定的工作,例如钻机和焊机。毛坯零件会进入工厂,然后装配线将其转换并组装为成品。

聚合管道是装配线,聚合阶段是装配站,操作符表达式则是专用工具。

您可以使用查找操作执行以下动作:

  • 选择要返回的文档

  • 选择要返回的字段

  • 对结果进行排序

您可以使用聚合操作执行以下动作:

  • 运行查找操作

  • 重命名字段

  • 计算字段

  • 汇总数据

  • 对值进行分组

使用聚合操作时,请记住以下限制:

  • Returned documents cannot violate the BSON document size limit of 16 megabytes.

  • 默认,管道阶段的内存限制为100 MB。 您可以通过将 实例的allow_disk_use 字段设置为 来超过此限制。mongocxx::options::aggregatetrue

重要

$graphLookup 异常

The $graphLookup stage has a strict memory limit of 100 megabytes and ignores the allow_disk_use field.

注意

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 Get Started with Atlas guide.

要执行聚合,请将包含聚合阶段的mongocxx::pipeline实例传递给collection.aggregate()方法。

以下代码示例计算纽约每个区的面包店数量。 为此,它使用包含以下阶段的聚合管道:

  • $match stage to filter for documents in which the cuisine field contains the value "Bakery"

  • $group stage to group the matching documents by the borough field, accumulating a count of documents for each distinct value

mongocxx::pipeline stages;
stages.match(make_document(kvp("cuisine", "Bakery")))
.group(make_document(kvp("_id", "$borough"), kvp("count", make_document(kvp("$sum", 1)))));
auto cursor = collection.aggregate(stages);
for (auto&& doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
{ "_id" : "Brooklyn", "count" : 173 }
{ "_id" : "Queens", "count" : 204 }
{ "_id" : "Bronx", "count" : 71 }
{ "_id" : "Staten Island", "count" : 20 }
{ "_id" : "Missing", "count" : 2 }
{ "_id" : "Manhattan", "count" : 221 }

要查看有关MongoDB如何执行您的操作的信息,您可以指示MongoDB查询规划器对其进行解释。 MongoDB解释操作时,会返回执行计划和性能统计信息。 执行计划是MongoDB完成操作的一种潜在方式。 当您指示MongoDB解释一个操作时,它会返回MongoDB执行的计划和任何被拒绝的执行计划。

要解释聚合操作,请运行explain数据库命令,具体方法是在BSON文档中指定该命令并将其作为参数传递给run_command()方法。

The following example instructs MongoDB to explain the aggregation operation from the preceding Aggregation Example:

mongocxx::pipeline stages;
stages.match(make_document(kvp("cuisine", "Bakery")))
.group(make_document(kvp("_id", "$borough"), kvp("count", make_document(kvp("$sum", 1)))));
auto command = make_document(
kvp("explain", make_document(
kvp("aggregate", "restaurants"),
kvp("pipeline", stages.view_array()),
kvp("cursor", make_document()))));
auto result = db.run_command(command.view());
std::cout << bsoncxx::to_json(result) << std::endl;
{ "explainVersion" : "2", "queryPlanner" : { "namespace" : "sample_restaurants.restaurants",
"indexFilterSet" : false, "parsedQuery" : { "cuisine" : { "$eq" : "Bakery" } }, "queryHash":
"...", "planCacheKey" : "...", "optimizedPipeline" : true, "maxIndexedOrSolutionsReached":
false, "maxIndexedAndSolutionsReached" : false, "maxScansToExplodeReached" : false,
"winningPlan" : { ... }
... }

For a full list of aggregation stages, see Aggregation Stages in the MongoDB Server manual.

要了解如何组装聚合管道并查看示例,请参阅聚合管道。

要了解有关解释 MongoDB 操作的更多信息,请参阅解释输出和查询计划。

有关使用C++驱动程序执行聚合操作的更多信息,请参阅以下API文档: