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

聚合(Aggregation)

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

您可以使用聚合操作来进程MongoDB集合中的数据并返回计算结果。 MongoDB聚合框架是 Query API的一部分,以数据处理管道的概念为模型。 文档进入包含一个或多个阶段的管道,每个阶段都会转换文档以输出最终的聚合结果。

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

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

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

  • 选择要返回的文档

  • 选择要返回的字段

  • 对结果进行排序

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

  • 执行查找操作

  • 重命名字段

  • 计算字段

  • 汇总数据

  • 对值进行分组

应用聚合操作时存在以下限制:

  • 返回的文档不得违反 MB 的BSON文档大小限制。16

  • 默认下,管道阶段的内存限制为 100 MB。您可以通过将 allowDiskUse 选项设置为 true 来超过此限制。

重要

$graphLookup 异常

$graphLookup 阶段有 MB100 的严格内存限制,并忽略allowDiskUse 选项。

本指南中的示例使用来自 Atlas 示例数据集sample_restaurants 数据库中的 restaurants 集合。要学习如何创建免费的MongoDB Atlas 集群并加载示例数据集,请参阅MongoDB 入门指南

提示

完成聚合教程

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 from the Select your language drop-down menu in the upper-right corner of the page.

要对集合中的文档执行聚合,请将表示管道阶段的 bson_t 结构传递给 mongoc_collection_aggregate() 函数。

此示例输出纽约市每个行政区的面包店数量。 以下代码创建一个包含以下阶段的聚合管道:

  • A $match stage to filter for documents in which the value of the cuisine field is "Bakery".

  • A $group stage to group the matching documents by the borough field, producing a count of documents for each distinct value of that field.

const bson_t *doc;
bson_t *pipeline = BCON_NEW("pipeline",
"[",
"{", "$match", "{", "cuisine", BCON_UTF8("Bakery"), "}", "}",
"{", "$group", "{",
"_id", BCON_UTF8("$borough"), "count", "{", "$sum", BCON_INT32(1), "}", "}",
"}",
"]");
mongoc_cursor_t *results =
mongoc_collection_aggregate(collection, MONGOC_QUERY_NONE, pipeline, NULL, NULL);
bson_error_t error;
if (mongoc_cursor_error(results, &error))
{
fprintf(stderr, "Aggregate failed: %s\n", error.message);
} else {
while (mongoc_cursor_next(results, &doc)) {
char *str = bson_as_canonical_extended_json(doc, NULL);
printf("%s\n", str);
bson_free(str);
}
}
bson_destroy(pipeline);
mongoc_cursor_destroy(results);

要查看有关MongoDB如何执行操作的信息,可以在管道上运行explain 操作。 MongoDB解释操作时,会返回执行计划和性能统计信息。执行计划是MongoDB完成操作的一种潜在方式。 当您指示MongoDB解释一个操作时,它会返回MongoDB为该操作选择的计划以及任何被拒绝的执行计划。

以下代码示例运行上一节所示的相同聚合,但使用 mongoc_client_command_simple() 函数来解释操作详细信息:

bson_t reply;
bson_error_t error;
bson_t *command = BCON_NEW(
"aggregate", BCON_UTF8("restaurants"),
"explain", BCON_BOOL(true),
"pipeline",
"[",
"{", "$match", "{", "cuisine", BCON_UTF8("Bakery"), "}", "}",
"{", "$group", "{",
"_id", BCON_UTF8("$borough"), "count", "{", "$sum", BCON_INT32(1), "}", "}",
"}",
"]");
if (mongoc_client_command_simple(client, "sample_restaurants", command, NULL, &reply, &error)) {
char *str = bson_as_canonical_extended_json(&reply, NULL);
printf("%s\n", str);
bson_free(str);
} else {
fprintf(stderr, "Command failed: %s\n", error.message);
}
bson_destroy(command);
bson_destroy(&reply);

有关聚合阶段的完整列表,请参阅 聚合阶段,详见 MongoDB Server 手册。

要学习;了解如何组装聚合管道并查看示例,请参阅MongoDB Server手册中的聚合管道

要学习;了解有关解释MongoDB操作的更多信息,请参阅MongoDB Server手册中的解释输出查询计划

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