发布于 2015-09-14 14:43:25 | 210 次阅读 | 评论: 0 | 来源: 网络整理
MongoDB provides flexible data aggregation functionality with the aggregate command. For additional information about aggregation consider the following resources:
This document provides a number of practical examples that display the capabilities of the aggregation framework. All examples use a publicly available data set of all zipcodes and populations in the United States.
To run you will need the zipcode data set. These data are available at: media.mongodb.org/zips.json. Use mongoimport to load this data set into your mongod instance.
集合里的文档样式:
{
"_id": "10280",
"city": "NEW YORK",
"state": "NY",
"pop": 5574,
"loc": [
-74.016323,
40.710537
]
}
在这些文档里:
以下所有实例在 mongo 壳里使用 aggregate() 助手。 aggregate() provides a wrapper around the aggregate database command. See the documentation for your driver for a more idiomatic interface for data aggregation operations.
要返回所有人口超过1000万的州,使用下列聚合操作:
db.zipcodes.aggregate( { $group :
{ _id : "$state",
totalPop : { $sum : "$pop" } } },
{ $match : {totalPop : { $gte : 10*1000*1000 } } } )
聚合操作使用 aggregate() 助手, 在 zipcodes 集合里处理所有文档。 aggregate() 一些 管道 算子定义聚合过程.
在以上实例中, 管道通过以下步骤运送所有 zipcodes 集合里的文档:
$group 运算收集所有文档并为每个州创建文档。
These new per-state documents have one field in addition the _id field: totalPop which is a generated field using the $sum operation to calculate the total value of all pop fields in the source documents.
$group 在管道中操作文档之后返回如下内容:
{
"_id" : "AK",
"totalPop" : 550043
}
$match 运算过滤这些文档,只剩下那些 totalPop 大于一千万的。
这个运算等效于 SQL :
SELECT state, SUM(pop) AS pop
FROM zips
GROUP BY state
HAVING pop > (10*1000*1000)
要返回每个州的城市平均人口,请使用以下聚合操作:
db.zipcodes.aggregate( { $group :
{ _id : { state : "$state", city : "$city" },
pop : { $sum : "$pop" } } },
{ $group :
{ _id : "$_id.state",
avgCityPop : { $avg : "$pop" } } } )
Aggregations operations using the aggregate() helper, process all documents on the zipcodes collection. aggregate() a number of pipeline operators that define the aggregation process.
In the above example, the pipeline passes all documents in the zipcodes collection through the following steps:
the $group operator collects all documents and creates new documents for every combination of the city and state fields in the source document.
After this stage in the pipeline, the documents resemble the following:
{
"_id" : {
"state" : "CO",
"city" : "EDGEWATER"
},
"pop" : 13154
}
the second $group operator collects documents by the state field and use the $avg expression to compute a value for the avgCityPop field.
The final output of this aggregation operation is:
{
"_id" : "MN",
"avgCityPop" : 5335
},
要返回每个州人口最大最小城市,使用下列聚合操作:
db.zipcodes.aggregate( { $group:
{ _id: { state: "$state", city: "$city" },
pop: { $sum: "$pop" } } },
{ $sort: { pop: 1 } },
{ $group:
{ _id : "$_id.state",
biggestCity: { $last: "$_id.city" },
biggestPop: { $last: "$pop" },
smallestCity: { $first: "$_id.city" },
smallestPop: { $first: "$pop" } } },
// the following $project is optional, and
// modifies the output format.
{ $project:
{ _id: 0,
state: "$_id",
biggestCity: { name: "$biggestCity", pop: "$biggestPop" },
smallestCity: { name: "$smallestCity", pop: "$smallestPop" } } } )
Aggregations operations using the aggregate() helper, process all documents on the zipcodes collection. aggregate() a number of pipeline operators that define the aggregation process.
All documents from the zipcodes collection pass into the pipeline, which consists of the following steps:
the $group operator collects all documents and creates new documents for every combination of the city and state fields in the source documents.
By specifying the value of _id as a sub-document that contains both fields, the operation preserves the state field for use later in the pipeline. The documents produced by this stage of the pipeline have a second field, pop, which uses the $sum operator to provide the total of the pop fields in the source document.
At this stage in the pipeline, the documents resemble the following:
{
"_id" : {
"state" : "CO",
"city" : "EDGEWATER"
},
"pop" : 13154
}
$sort operator orders the documents in the pipeline based on the vale of the pop field from largest to smallest. This operation does not alter the documents.
the second $group operator collects the documents in the pipeline by the state field, which is a field inside the nested _id document.
Within each per-state document this $group operator specifies four fields: Using the $last expression, the $group operator creates the biggestcity and biggestpop fields that store the city with the largest population and that population. Using the $first expression, the $group operator creates the smallestcity and smallestpop fields that store the city with the smallest population and that population.
The documents, at this stage in the pipeline resemble the following:
{
"_id" : "WA",
"biggestCity" : "SEATTLE",
"biggestPop" : 520096,
"smallestCity" : "BENGE",
"smallestPop" : 2
}
The final operation is $project, which renames the _id field to state and moves the biggestCity, biggestPop, smallestCity, and smallestPop into biggestCity and smallestCity sub-documents.
这个聚合操作最终输出:
{
"state" : "RI",
"biggestCity" : {
"name" : "CRANSTON",
"pop" : 176404
},
"smallestCity" : {
"name" : "CLAYVILLE",
"pop" : 45
}
}
假设一个体育俱乐部数据库,包含跟踪用户的加入时间,运动喜好 user 集合,存储这些数据到文档,类似下面这样的:
{
_id : "jane",
joined : ISODate("2011-03-02"),
likes : ["golf", "racquetball"]
}
{
_id : "joe",
joined : ISODate("2012-07-02"),
likes : ["tennis", "golf", "swimming"]
}
下面的操作将返回按字母顺序排列大写的用户名。聚合包含 users 集合所有文档的用户名。按名排序.
db.users.aggregate(
[
{ $project : { name:{$toUpper:"$_id"} , _id:0 } },
{ $sort : { name : 1 } }
]
)
来自 users 集合全部文档穿过管道, 包含一下操作:
聚合的结果将类似于以下:
{
"name" : "JANE"
},
{
"name" : "JILL"
},
{
"name" : "JOE"
}
以下聚合操作返回按他们加入的月份排序的用户名。这个类型的聚合能帮助生成会员更新通知。
db.users.aggregate(
[
{ $project : { month_joined : {
$month : "$joined"
},
name : "$_id",
_id : 0
},
{ $sort : { month_joined : 1 } }
]
)
管道通过以下操作传输所有 users 集合里的文档:
该操作返回的结果类似于下面这样的:
{
"month_joined" : 1,
"name" : "ruth"
},
{
"month_joined" : 1,
"name" : "harold"
},
{
"month_joined" : 1,
"name" : "kate"
}
{
"month_joined" : 2,
"name" : "jill"
}
以下操作展示一年中每月多少人加入。您可以为招聘和营销策略等信息使用这些汇总数据。
db.users.aggregate(
[
{ $project : { month_joined : { $month : "$joined" } } } ,
{ $group : { _id : {month_joined:"$month_joined"} , number : { $sum : 1 } } },
{ $sort : { "_id.month_joined" : 1 } }
]
)
管道通过以下操作传输所有 users 集合里的文档:
该操作返回的结果类似于下面这样的:
{
"_id" : {
"month_joined" : 1
},
"number" : 3
},
{
"_id" : {
"month_joined" : 2
},
"number" : 9
},
{
"_id" : {
"month_joined" : 3
},
"number" : 5
}
The following aggregation collects top five most “liked” activities in the data set. In this data set, you might use an analysis of this to help inform planning and future development.
db.users.aggregate(
[
{ $unwind : "$likes" },
{ $group : { _id : "$likes" , number : { $sum : 1 } } },
{ $sort : { number : -1 } },
{ $limit : 5 }
]
)
管道通过以下操作传输所有 users 集合里的文档:
$unwind 算子 分开 each value in the likes array, and creates a new version of the source document for every element in the array.
Example
Given the following document from the users collection:
{
_id : "jane",
joined : ISODate("2011-03-02"),
likes : ["golf", "racquetball"]
}
$unwind 算子 would create the following documents:
{
_id : "jane",
joined : ISODate("2011-03-02"),
likes : "golf"
}
{
_id : "jane",
joined : ISODate("2011-03-02"),
likes : "racquetball"
}
$group 算子 collects all documents the same value for the likes field and counts each grouping. With this information, $group creates a new document with two fields:
$sort 算子 sorts these documents by the number field in reverse order.
$limit 算子 only includes the first 5 result documents.
该操作返回的结果类似于下面这样的:
{
"_id" : "golf",
"number" : 33
},
{
"_id" : "racquetball",
"number" : 31
},
{
"_id" : "swimming",
"number" : 24
},
{
"_id" : "handball",
"number" : 19
},
{
"_id" : "tennis",
"number" : 18
}