Overview
在本指南中,您可以了解如何使用 Ruby 驱动程序来执行聚合操作。
聚合操作处理 MongoDB 集合中的数据并返回计算结果。 MongoDB 聚合框架是 Query API 的一部分,以数据处理管道的概念为模型。 文档进入包含一个或多个阶段的管道,该管道将文档转换为聚合结果。
类比
聚合操作类似于汽车工厂。汽车工厂有一条装配线,其中包含配备专用工具的装配站,用于完成特定的工作,例如钻机和焊机。毛坯零件会进入工厂,然后装配线将其转换并组装为成品。
聚合管道是装配线,聚合阶段是装配站,操作符表达式则是专用工具。
比较聚合与查找操作
下表列出了查找操作可以执行的不同任务,并将它们与聚合操作可以执行的任务进行了比较。 聚合框架提供了扩展功能,允许您转换和操作数据。
查找操作 | 聚合操作 |
|---|---|
选择要返回的特定文档 |
|
限制
执行聚合操作时要考虑以下限制:
返回的文档不能违反16 MB 的BSON文档大小限制。
默认下,管道阶段的内存限制为 100 MB。 您可以通过将
true值传递给allow_disk_use方法并将该方法链接到aggregate来超出此限制。$graphLookup操作符有 100兆字节的严格内存限制,并忽略传递给
allow_disk_use方法的值。
运行聚合操作
注意
样本数据
本指南中的示例使用Atlas示例数据集的sample_restaurants数据库中的restaurants集合。 要学习;了解如何创建免费的MongoDB Atlas 群集并加载示例数据集,请参阅Atlas入门指南。
要执行聚合,请将每个管道阶段定义为Ruby hash,然后将操作管道传递给 aggregate 方法。
聚合示例
以下代码示例计算纽约每个区的面包店数量。 为此,它使用具有以下阶段的聚合管道:
database = client.use('sample_restaurants') restaurants_collection = database[:restaurants] pipeline = [ { '$match' => { 'cuisine' => 'Bakery' } }, { '$group' => { '_id' => '$borough', 'count' => { '$sum' => 1 } } } ] aggregation = restaurants_collection.aggregate(pipeline) aggregation.each do |doc| puts doc end
{"_id"=>"Bronx", "count"=>71} {"_id"=>"Manhattan", "count"=>221} {"_id"=>"Queens", "count"=>204} {"_id"=>"Missing", "count"=>2} {"_id"=>"Staten Island", "count"=>20} {"_id"=>"Brooklyn", "count"=>173}
解释聚合
要查看有关MongoDB如何执行您的操作的信息,您可以指示MongoDB 查询规划器对其进行解释。MongoDB解释操作时,会返回执行计划和性能统计信息。执行计划是MongoDB完成操作的一种潜在方式。当您指示MongoDB解释一个操作时,默认下它会返回MongoDB执行的计划和任何被拒绝的执行计划。
要解释聚合操作,请将 explain 方法链接到 aggregate 方法。
以下示例指示MongoDB解释前面聚合示例中的聚合操作:
explanation = restaurants_collection.aggregate(pipeline).explain() puts explanation
{"explainVersion"=>"2", "queryPlanner"=>{"namespace"=>"sample_restaurants.restaurants", "parsedQuery"=>{"cuisine"=> {"$eq"=> "Bakery"}}, "indexFilterSet"=>false, "planCacheKey"=>"6104204B", "optimizedPipeline"=>true, "maxIndexedOrSolutionsReached"=>false, "maxIndexedAndSolutionsReached"=>false, "maxScansToExplodeReached"=>false, "prunedSimilarIndexes"=>false, "winningPlan"=>{"isCached"=>false, "queryPlan"=>{"stage"=>"GROUP", "planNodeId"=>3, "inputStage"=>{"stage"=>"COLLSCAN", "planNodeId"=>1, "filter"=>{}, "direction"=>"forward"}},...}
运行全文搜索
要指定一个或多个字段的全文搜索,您可以使用MongoDB 搜索创建 $search 管道阶段。
此示例创建管道阶段来执行以下操作:
在
name术语中搜索"Salt"字段仅投影匹配文档的
_id和name值
重要
To run the following example, you must create a MongoDB Search index on the restaurants collection that covers the name field. Then, replace the "<your_search_index_name>" placeholder with the name of the index. To learn how to create a MongoDB Search index, see the MongoDB Search and MongoDB Vector Search Indexes guide.
search_pipeline = [ { '$search' => { 'index' => '<your_search_index_name>', 'text' => { 'query' => 'Salt', 'path' => 'name' }, } }, { '$project' => { '_id' => 1, 'name' => 1 } } ] results = collection.aggregate(search_pipeline) results.each do |document| puts document end
{"_id"=> {"$oid"=> "..."}, "name"=> "Fresh Salt"} {"_id"=> {"$oid"=> "..."}, "name"=> "Salt & Pepper"} {"_id"=> {"$oid"=> "..."}, "name"=> "Salt + Charcoal"} {"_id"=> {"$oid"=> "..."}, "name"=> "A Salt & Battery"} {"_id"=> {"$oid"=> "..."}, "name"=> "Salt And Fat"} {"_id"=> {"$oid"=> "..."}, "name"=> "Salt And Pepper Diner"}
更多信息
MongoDB Server 手册
要学习;了解有关本指南所讨论主题的更多信息,请参阅MongoDB Server手册中的以下页面:
API 文档
要学习;了解有关Ruby驱动程序聚合方法的更多信息,请参阅聚合的API文档。