MongoDB 聚合语句

MongoDB中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果。有点类似sql语句中的 count(*)。

基本语法为:db.collection.aggregate( [ , , ... ] )

现在在mycol集合中有以下数据:

{ "_id" : 1, "name" : "tom", "sex" : "男", "score" : 100, "age" : 34 }

{ "_id" : 2, "name" : "jeke", "sex" : "男", "score" : 90, "age" : 24 }

{ "_id" : 3, "name" : "kite", "sex" : "女", "score" : 40, "age" : 36 }

{ "_id" : 4, "name" : "herry", "sex" : "男", "score" : 90, "age" : 56 }

{ "_id" : 5, "name" : "marry", "sex" : "女", "score" : 70, "age" : 18 }

{ "_id" : 6, "name" : "john", "sex" : "男", "score" : 100, "age" : 31 }

1、$sum 计算总和。

Sql: select sex,count(*) from mycol group by sex

MongoDb: db.mycol.aggregate([{group: {_id: 'sex', personCount: {$sum: 1}}}])

Sql: select sex,sum(score) totalScore from mycol group by sex

MongoDb: db.mycol.aggregate([{group: {_id: 'sex', totalScore: {sum:′score'}}}])

2、$avg 计算平均值

Sql: select sex,avg(score) avgScore from mycol group by sex

Mongodb: db.mycol.aggregate([{group: {_id: 'sex', avgScore: {avg:′score'}}}])

3、$max 获取集合中所有文档对应值得最大值。

Sql: select sex,max(score) maxScore from mycol group by sex

Mongodb: db.mycol.aggregate([{group: {_id: 'sex', maxScore : {max:′score'}}}])

4、$min 获取集合中所有文档对应值得最小值。

Sql: select sex,min(score) minScore from mycol group by sex

Mongodb: db.mycol.aggregate([{group: {_id: 'sex', minScore : {min:′score'}}}])

5、$push 把文档中某一列对应的所有数据插入值到一个数组中。

Mongodb: db.mycol.aggregate([{group: {_id: 'sex', scores : {push:′score'}}}])

6、$addToSet 把文档中某一列对应的所有数据插入值到一个数组中,去掉重复的

db.mycol.aggregate([{group: {_id: 'sex', scores : {addToSet:′score'}}}])

7、 $first 根据资源文档的排序获取第一个文档数据。

db.mycol.aggregate([{group: {_id: 'sex', firstPerson : {first:′name'}}}])

8、 $last 根据资源文档的排序获取最后一个文档数据。

db.mycol.aggregate([{group: {_id: 'sex', lastPerson : {last:′name'}}}])

9、全部统计 null

db.mycol.aggregate([{group:{_id:null,totalScore:{push:'$score'}}}])

例子

现在在t2集合中有以下数据:

{ "country" : "china", "province" : "sh", "userid" : "a" }

{ "country" : "china", "province" : "sh", "userid" : "b" }

{ "country" : "china", "province" : "sh", "userid" : "a" }

{ "country" : "china", "province" : "sh", "userid" : "c" }

{ "country" : "china", "province" : "bj", "userid" : "da" }

{ "country" : "china", "province" : "bj", "userid" : "fa" }

需求是统计出每个country/province下的userid的数量(同一个userid只统计一次)

过程如下。

首先试着这样来统计:

db.t2.aggregate([ { group: {"_id": { "country" : "country", "prov": "province"} , "number":{sum:1}} } ])

结果是错误的:

原因是,这样来统计不能区分userid相同的情况 (上面的数据中sh有两个 userid = a)

为了解决这个问题,首先执行一个group,其id 是 country, province, userid三个field:

db.t2.aggregate([ { group: {"_id": { "country" : "country", "province": "province","uid":"userid" } } } ])

可以看出,这步的目的是把相同的userid只剩下一个。

然后第二步,再第一步的结果之上再执行统计:

db.t2.aggregate([

{ group: {"_id": { "country" : "country", "province": "province","uid":"userid" } } } ,

{ group: {"_id": { "country" : "_id.country", "province": "_id.province" }, count : {sum : 1 } } }

])

这回就对了

加入一个$project操作符,把_id去掉

db.t2.aggregate([ { group: {"_id": { "country" : "country", "province": "province","uid":"userid" } } } ,

{ group: {"_id": { "country" : "_id.country", "province": "_id.province" }, count: {sum : 1 } } },

{ project : {"_id": 0, "country" : "_id.country", "province" : "$_id.province", "count" : 1}}

])

管道的概念

管道在Unix和Linux中一般用于将当前命令的输出结果作为下一个命令的参数。

MongoDB的聚合管道将MongoDB文档在一个管道处理完毕后将结果传递给下一个管道处理。管道操作是可以重复的。

表达式:处理输入文档并输出。表达式是无状态的,只能用于计算当前聚合管道的文档,不能处理其它的文档。

这里我们介绍一下聚合框架中常用的几个操作:

1、$project实例

db.mycol.aggregate({$project:{name : 1, score : 1}})

这样的话结果中就只还有_id,name和score三个字段了,默认情况下_id字段是被包含的,如果要想不包含_id话可以这样:

db.mycol.aggregate({$project:{_id : 0, name : 1, score : 1}})

2、$match实例

match用于获取分数大于30小于并且小于100的记录,然后将符合条件的记录送到下一阶段group管道操作符进行处理

db.mycol.aggregate([{match :{score: {gt: 30, lt: 100}}},{group:{_id:'sex',count:{sum:1}}}])

展开阅读全文

页面更新:2024-05-27

标签:嵌套   最大值   平均值   数组   表达式   字段   语句   管道   个数   实例   命令   数量   操作   文档   数据   资源   科技

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top