
站在用户的角度思考问题,与客户深入沟通,找到平桥网站设计与平桥网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站建设、成都做网站、企业官网、英文网站、手机端网站、网站推广、域名注册、雅安服务器托管、企业邮箱。业务覆盖平桥地区。
aggregate() 方法
您可以使用 MongoDB 中的 aggregate() 方法来执行聚合操作,其语法格式如下:
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"}}}]) |
管道
在 UNIX 命令中,管道意味着可以将某些操作的输出结果作为下一个命令的参数,以此类推。MongoDB 中同样也支持管道,即 MongoDB 会在一个管道处理完毕后将结果传递给下一个管道处理,而且管道操作是可以重复的。
下面介绍了聚合框架中几个常用的操作:
- $project:用于从集合中选择要输出的字段;
- $match:用于过滤数据,只输出符合条件的文档,可以减少作为下一阶段输入的文档数量;
- $group:对集合中的文档进行分组,可用于统计结果;
- $sort:将输入文档进行排序后输出;
- $skip:在聚合管道中跳过指定数量的文档,并返回余下的文档;
- $limit:用来限制 MongoDB 聚合管道返回的文档数量;
- $unwind:将文档中的某一个数组类型字段拆分成多条,每条包含数组中的一个值。
下面通过几个简单的示例来演示 MongoDB 中管道的使用:
1) $project
【示例】使用 $project 来选择要输出的字段:
> 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" : "编程帮" }2) $skip
【示例】使用 $skip 跳过指定数量的文档:
> db.course.aggregate({$skip:2}).pretty()
{
"_id" : ObjectId("60331a7eee79704753940393"),
"title" : "MongoDB教程",
"author" : "编程帮",
"url" : "http://www.biancheng.com/mongodb/index.html"
}分享文章:MongoDB聚合查询
文章网址:http://www.jxjierui.cn/article/dhogddc.html


咨询
建站咨询
