使用聚合转换您的数据
概述
在本指南中,您可以学习如何使用C驱动程序执行 聚合操作。
您可以使用聚合操作处理您的MongoDB集合中的数据,并返回计算结果。MongoDB聚合框架是查询API的一部分,其设计基于数据处理管道的概念。文档进入一个包含一个或多个阶段的管道,每个阶段将文档转换以输出最终的聚合结果。
您可以将聚合操作想象成一个汽车工厂。汽车工厂有一条装配线,其中包含装配站,装配站有专门的工具来完成特定的工作,例如钻床和焊接机。原始部件进入工厂,然后装配线将其转换和组装成成品。
聚合管道是装配线,聚合阶段是装配站,而操作表达式是专门的工具。
比较聚合操作和查找操作
您可以使用查找操作执行以下操作
选择要返回的文档
选择要返回的字段
排序结果
您可以使用聚合操作执行以下操作
执行查找操作
重命名字段
计算字段
汇总数据
分组值
限制条件
使用聚合操作时存在以下限制条件
返回的文档必须不违反BSON 文档大小限制为16兆字节。
默认情况下,管道阶段的内存限制为100兆字节。您可以通过设置
allowDiskUse
选项为true
来超过此限制。
聚合示例
本节中的示例使用来自Atlas样本数据集的sample_restaurants
数据库中的restaurants
集合。要了解如何创建免费的MongoDB Atlas集群并加载数据集,请参阅Atlas入门指南。
构建和执行聚合管道
要对集合中的文档执行聚合,将表示管道阶段的bson_t
结构传递给mongoc_collection_aggregate()
函数。
以下示例输出了纽约市每个区面包店的数量。以下代码创建了一个包含以下阶段的聚合管道:
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);
{ "_id" : "Queens", "count" : { "$numberInt" : "204" } } { "_id" : "Staten Island", "count" : { "$numberInt" : "20" } } { "_id" : "Missing", "count" : { "$numberInt" : "2" } } { "_id" : "Bronx", "count" : { "$numberInt" : "71" } } { "_id" : "Brooklyn", "count" : { "$numberInt" : "173" } } { "_id" : "Manhattan", "count" : { "$numberInt" : "221" } }
解释聚合
要查看有关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);
{ "explainVersion": "2", "queryPlanner": { "namespace": "sample_restaurants.restaurants" "indexFilterSet": false, "parsedQuery": { "cuisine": {"$eq": "Bakery"} }, "queryHash": "865F14C3", "planCacheKey": "0697561B", "optimizedPipeline": true, "maxIndexedOrSolutionsReached": false, "maxIndexedAndSolutionsReached": false, "maxScansToExplodeReached": false, "winningPlan": { ... }, "rejectedPlans": [] ... } ... }
更多信息
要查看表达式运算符的完整列表,请参阅 MongoDB 服务器手册中的聚合运算符。
要了解如何构建聚合管道并查看示例,请参阅 MongoDB 服务器手册中的聚合管道。
要了解有关创建管道阶段的更多信息,请参阅 MongoDB 服务器手册中的聚合阶段。
要了解更多有关解释 MongoDB 操作的信息,请参阅 MongoDB 服务器手册中的解释输出 和 查询计划。
API 文档
有关使用 C 驱动程序执行聚合操作的更多信息,请参阅以下 API 文档