聚合
概述
在本指南中,您可以学习如何使用 MongoDB .NET/C# 驱动程序执行 聚合操作。
聚合操作处理您的 MongoDB 集合中的数据,并返回计算结果。MongoDB 聚合框架基于数据处理管道的概念。文档进入由一个或多个阶段组成的管道,该管道将文档转换为一个聚合结果。
类比
聚合操作类似于汽车工厂的流水线。流水线上的各个工作站有专门的工具来执行特定的任务。例如,在制造汽车时,流水线从车架开始。然后,随着车架在流水线上移动,每个工作站组装不同的部件。最终结果是转换后的最终产品,即完成的汽车。
流水线代表 聚合管道,各个工作站代表 聚合阶段,专门的工具代表 表达式运算符,而最终产品代表 聚合结果。
比较聚合和查找操作
下表列出了您可以使用查找操作执行的不同任务,以及您可以使用聚合操作实现的目标。聚合框架提供了扩展的功能,允许您转换和操作您的数据。
查找操作 | 聚合操作 |
---|---|
选择要返回的特定文档 选择要返回的字段 对结果进行排序 限制结果数量 统计结果数量 | 选择要返回的特定文档 选择要返回的字段 对结果进行排序 限制结果数量 统计结果数量 对结果进行分组 重命名字段 计算新字段 汇总数据 连接和合并数据集 |
服务器限制
请考虑以下限制,在执行聚合操作时
返回的文档不得违反16兆字节的BSON文档大小限制。
默认情况下,管道阶段具有100兆字节的内存限制。如果需要,您可以通过设置传递给
Aggregate()
方法的AggregateOptions
对象的AllowDiskUse属性来超过此限制。AggregateOptions
对象。$graphLookup阶段具有严格的100兆字节内存限制,并忽略
AllowDiskUse
属性。
聚合示例
要执行聚合,请将聚合阶段列表传递给 IMongoCollection<TDocument>.Aggregate()
方法。
注意
此示例使用 Atlas 示例数据集 中的 sample_restaurants.restaurants
集合。要了解如何创建免费的 MongoDB Atlas 集群并加载示例数据集,请参阅快速入门.
以下代码示例生成了纽约市每个区面包店的数量。为此,它使用包含以下阶段的聚合管道
以下部分通过使用 LINQ、Builders 和 BsonDocument 方法来实现此示例,这些方法用于创建和组合示例管道中使用的聚合阶段。
LINQ 方法
// Defines a queryable collection object as a prerequisite to using LINQ var queryableCollection = collection.AsQueryable(); // Defines the query with $match and $group stages var query = queryableCollection .Where(r => r.Cuisine == "Bakery") .GroupBy(r => r.Borough) .Select(g => new { _id = g.Key, Count = g.Count() }); // Executes the query and prints the aggregated results foreach (var result in query.ToList()) { Console.WriteLine(result); }
{ _id = Bronx, Count = 71 } { _id = Brooklyn, Count = 173 } { _id = Staten Island, Count = 20 } { _id = Missing, Count = 2 } { _id = Manhattan, Count = 221 } { _id = Queens, Count = 204 }
要了解更多关于使用 LINQ 构建聚合管道的信息,请参阅 LINQ 指南。
构建器方法
// Defines the $match aggregation stage var matchFilter = Builders<Restaurant>.Filter.Eq(r => r.Cuisine, "Bakery"); // Defines the aggregation pipeline with the $match and $group aggregation stages var pipeline = new EmptyPipelineDefinition<Restaurant>() .Match(matchFilter) .Group(r => r.Borough, g => new { _id = g.Key, Count = g.Count() } ); // Executes the aggregation pipeline var results = collection.Aggregate(pipeline).ToList(); // Prints the aggregated results foreach (var result in results) { Console.WriteLine(result); }
{ _id = Bronx, Count = 71 } { _id = Brooklyn, Count = 173 } { _id = Staten Island, Count = 20 } { _id = Missing, Count = 2 } { _id = Manhattan, Count = 221 } { _id = Queens, Count = 204 }
要了解更多关于使用构建器构建聚合管道的信息,请参阅《使用构建器进行操作》指南中的构建聚合管道部分。
BsonDocument方法
// Defines the $match and $group aggregation stages var matchStage = new BsonDocument { { "$match", new BsonDocument { { "cuisine", "Bakery" } } } }; var groupStage = new BsonDocument { { "$group", new BsonDocument { { "_id", "$borough" }, { "count", new BsonDocument("$sum", 1) } } } }; // Executes the aggregation pipeline var pipeline = new[] { matchStage, groupStage }; var results = collection.Aggregate<BsonDocument>(pipeline).ToList(); // Prints the aggregated results foreach (BsonDocument result in results) { Console.WriteLine(result); }
{ "_id" : "Brooklyn", "count" : 173 } { "_id" : "Manhattan", "count" : 221 } { "_id" : "Bronx", "count" : 71 } { "_id" : "Missing", "count" : 2 } { "_id" : "Staten Island", "count" : 20 } { "_id" : "Queens", "count" : 204 }
更多信息
MongoDB服务器手册
要查看表达式运算符的完整列表,请参阅聚合运算符。
要了解更多关于聚合管道的组装和查看示例,请参阅聚合管道。
要了解更多关于创建管道阶段的信息,请参阅聚合阶段。
API 文档
关于本指南中讨论的聚合操作的更多信息,请参阅以下API文档