On this page本页内容
Consider a hypothetical sports club with a database that contains a 考虑一个假设的体育俱乐部,其数据库包含一个users
collection that tracks the user's join dates, sport preferences, and stores these data in documents that resemble the following:users
集合,跟踪用户的加入日期、体育偏好,并将这些数据存储在类似以下的文档中:
{ _id : "jane", joined : ISODate("2011-03-02"), likes : ["golf", "racquetball"] } { _id : "joe", joined : ISODate("2012-07-02"), likes : ["tennis", "golf", "swimming"] }
The following operation returns user names in upper case and in alphabetical order. 以下操作以大写字母顺序返回用户名。The aggregation includes user names for all documents in the 聚合包括users
collection. users
集合中所有文档的用户名。You might do this to normalize user names for processing.您可以这样做,以规范化要处理的用户名。
db.users.aggregate( [ { $project : { name:{$toUpper:"$_id"} , _id:0 } }, { $sort : { name : 1 } } ] )
All documents from the users
collection pass through the pipeline, which consists of the following operations:users
集合中的所有文档都通过管道,管道包括以下操作:
The $project
operator:$project
运算符:
name
.name
的新字段。_id
to upper case, with the $toUpper
operator. $toUpper
运算符将_id
的值转换为大写。$project
creates a new field, named name
to hold this value.$project
创建一个名为name
的新字段来保存该值。id
field. id
字段。$project
_id
field by default, unless explicitly suppressed._id
字段,除非显式抑制。$sort
operator orders the results by the name
field.$sort
运算符按name
字段对结果排序。The results of the aggregation would resemble the following:聚合结果如下:
{ "name" : "JANE" }, { "name" : "JILL" }, { "name" : "JOE" }
On this page在这一页
The following aggregation operation returns user names sorted by the month they joined. 以下聚合操作将返回按加入月份排序的用户名。This kind of aggregation could help generate membership renewal notices.这种聚合可以帮助生成成员资格续订通知。
db.users.aggregate( [ { $project : { month_joined : { $month : "$joined" }, name : "$_id", _id : 0 } }, { $sort : { month_joined : 1 } } ] )
The pipeline passes all documents in the 管道通过以下操作传递users
collection through the following operations:users
集合中的所有文档:
The $project
operator:
month_joined
and name
.month_joined
和name
。id
from the results. aggregate()
method includes the _id
, unless explicitly suppressed.aggregate()
方法包括_id
,除非显式抑制。$month
operator converts the values of the joined
field to integer representations of the month. $month
运算符将joined
字段的值转换为月份的整数表示。$project
operator assigns those values to the month_joined
field.$project
运算符将这些值分配给month_joined
字段。$sort
operator sorts the results by the month_joined
field.$sort
运算符按month_joined
字段对结果进行排序。The operation returns results that resemble the following:该操作将返回类似以下的结果:
{ "month_joined" : 1, "name" : "ruth" }, { "month_joined" : 1, "name" : "harold" }, { "month_joined" : 1, "name" : "kate" } { "month_joined" : 2, "name" : "jill" }
On this page在这一页
The following operation shows how many people joined each month of the year. 以下操作显示了一年中每个月有多少人加入。You might use this aggregated data for recruiting and marketing strategies.您可以将这些汇总数据用于招聘和营销策略。
db.users.aggregate( [ { $project : { month_joined : { $month : "$joined" } } } , { $group : { _id : {month_joined:"$month_joined"} , number : { $sum : 1 } } }, { $sort : { "_id.month_joined" : 1 } } ] )
The pipeline passes all documents in the 管道通过以下操作传递users
collection through the following operations:users
集合中的所有文档:
$project
operator creates a new field called month_joined
.$project
运算符创建一个名为month_joined
的新字段。$month
operator converts the values of the joined
field to integer representations of the month. $month
运算符将joined
字段的值转换为月份的整数表示。$project
operator assigns the values to the month_joined
field.$project
运算符将值分配给month_joined
字段。The $group
operator collects all documents with a given month_joined
value and counts how many documents there are for that value. $group
运算符集合具有给定month_joined
值的所有文档,并计算该值的文档数量。Specifically, for each unique value, 具体而言,对于每个唯一值,$group
creates a new "per-month" document with two fields:$group
创建一个新的“每月”文档,其中包含两个字段:
_id
month_joined
field and its value.month_joined
字段及其值。number
$sum
operator increments this field by 1 for every document containing the given month_joined
value.$sum
运算符为包含给定month_joined
值的每个文档将该字段递增1。$sort
operator sorts the documents created by $group
according to the contents of the month_joined
field.$sort
运算符根据month_joined
字段的内容对$group
创建的文档进行排序。The result of this aggregation operation would resemble the following:此聚合操作的结果如下:
{ "_id" : { "month_joined" : 1 }, "number" : 3 }, { "_id" : { "month_joined" : 2 }, "number" : 9 }, { "_id" : { "month_joined" : 3 }, "number" : 5 }
On this page在这一页
The following aggregation collects top five most "liked" activities in the data set. 下面的聚合集合了数据集中最受欢迎的前五个活动。This type of analysis could help inform planning and future development.这类分析有助于为规划和未来发展提供信息。
db.users.aggregate( [ { $unwind : "$likes" }, { $group : { _id : "$likes" , number : { $sum : 1 } } }, { $sort : { number : -1 } }, { $limit : 5 } ] )
The pipeline begins with all documents in the 管道从users
collection, and passes these documents through the following operations:users
集合中的所有文档开始,并通过以下操作传递这些文档:
The $unwind
operator separates each value in the likes
array, and creates a new version of the source document for every element in the array.$unwind
运算符分离likes
数组中的每个值,并为数组中的每一个元素创建源文档的新版本。
Given the following document from the 给定users
collection:users
集合中的以下文档:
{ _id : "jane", joined : ISODate("2011-03-02"), likes : ["golf", "racquetball"] }
The $unwind
operator would create the following documents:$unwind
运算符将创建以下文档:
{ _id : "jane", joined : ISODate("2011-03-02"), likes : "golf" } { _id : "jane", joined : ISODate("2011-03-02"), likes : "racquetball" }
The $group
operator collects all documents with the same value for the likes
field and counts each grouping. $group
运算符为likes
字段集合具有相同值的所有文档,并对每个分组进行计数。With this information, 有了这些信息,$group
creates a new document with two fields:$group
将创建一个包含两个字段的新文档:
$sort
operator sorts these documents by the number
field in reverse order.$sort
运算符按number
字段按相反顺序对这些文档进行排序。$limit
operator only includes the first 5 result documents.$limit
运算符仅包括前5个结果文档。The results of aggregation would resemble the following:聚合结果如下:
{ "_id" : "golf", "number" : 33 }, { "_id" : "racquetball", "number" : 31 }, { "_id" : "swimming", "number" : 24 }, { "_id" : "handball", "number" : 19 }, { "_id" : "tennis", "number" : 18 }