365Tools
发布时间:2024-03-29 18:30:01
MongoDB 中的聚合操作用来处理数据并返回计算结果,聚合操作可以将多个文档中的值组合在一起,并可对数据执行各种操作,以返回单个结果,有点类似于 SQL 语句中的 count(*)、group by 等。db.collection_name.aggregate(aggregate_operation)
【示例】假设集合“course”中有如下数据:
> db.course.find().pretty()
{
"_id" : ObjectId("60331a7eee79704753940391"),
"title" : "HTML教程",
"author" : "编程帮",
"url" : "http://www.biancheng.com/html/index.html"
}
{
"_id" : ObjectId("60331a7eee79704753940392"),
"title" : "C#教程",
"author" : "编程帮",
"url" : "http://www.biancheng.com/csharp/index.html"
}
{
"_id" : ObjectId("60331a7eee79704753940393"),
"title" : "MongoDB教程",
"author" : "编程帮",
"url" : "http://www.biancheng.com/mongodb/index.html"
}
若要统计每个作者“author”一共编写了多少教程,可以使用下面的 aggregate() 方法:
> db.course.aggregate([{$group : {_id : "$author", sum : {$sum : 1}}}])
{ "_id" : "编程帮", "sum" : 3 }
上述示例类似于 SQL 语句中的SELECT author, count(*) FROM course GROUP BY author。| 表达式 | 描述 | 实例 |
|---|---|---|
| $sum | 计算总和 | db.mycol.aggregate([{$group : {_id : "$author", num_tutorial : {$sum : "$likes"}}}]) |
| $avg | 计算平均值 | db.mycol.aggregate([{$group : {_id : "$author", num_tutorial : {$avg : "$likes"}}}]) |
| $min | 获取集合中所有文档对应值得最小值 | db.mycol.aggregate([{$group : {_id : "$author", num_tutorial : {$min : "$likes"}}}]) |
| $max | 获取集合中所有文档对应值得最大值 | db.mycol.aggregate([{$group : {_id : "$author", num_tutorial : {$max : "$likes"}}}]) |
| $push | 在结果文档中插入值到一个数组中 | db.mycol.aggregate([{$group : {_id : "$author", url : {$push: "$url"}}}]) |
| $addToSet | 在结果文档中插入值到一个数组中,但不创建副本 | db.mycol.aggregate([{$group : {_id : "$author", url : {$addToSet : "$url"}}}]) |
| $first | 根据资源文档的排序获取第一个文档数据 | db.mycol.aggregate([{$group : {_id : "$author", first_url : {$first : "$url"}}}]) |
| $last | 根据资源文档的排序获取最后一个文档数据 | db.mycol.aggregate([{$group : {_id : "$author", last_url : {$last : "$url"}}}]) |
> db.course.aggregate({$project:{title:1, author:1}}).pretty()
{
"_id" : ObjectId("60331a7eee79704753940391"),
"title" : "HTML教程",
"author" : "编程帮"
}
{
"_id" : ObjectId("60331a7eee79704753940392"),
"title" : "C#教程",
"author" : "编程帮"
}
{
"_id" : ObjectId("60331a7eee79704753940393"),
"title" : "MongoDB教程",
"author" : "编程帮"
}
通过运行结果可以看出,文档中的 _id 字段默认是选中的,如果不想显示 _id 字段的话,可以像下面这样:
> db.course.aggregate({$project:{_id:0, title:1, author:1}}).pretty()
{ "title" : "HTML教程", "author" : "编程帮" }
{ "title" : "C#教程", "author" : "编程帮" }
{ "title" : "MongoDB教程", "author" : "编程帮" }
> db.course.aggregate({$skip:2}).pretty()
{
"_id" : ObjectId("60331a7eee79704753940393"),
"title" : "MongoDB教程",
"author" : "编程帮",
"url" : "http://www.biancheng.com/mongodb/index.html"
}