【Mongo】mongo聚合操作

1.mark 记个笔记
  1. db.getCollection('redefine-collection').aggregate([
  2.     { $match:{ 'stime': {'$gte':1526486400000, '$lt':1526572800000},'event_id': '0101001','event_info.action': {'$in': [1, 4,6,9,16,18,21,23,24]} } },
  3.     { $group:{ _id:"$event_info.action",total:{ $sum:"$page_source"} } } ####分组求和
  4.     ])



  1. db.getCollection('redefine-collection').aggregate([
  2.     { $match:{ 'stime': {'$gte':1526486400000, '$lt':1526572800000},'event_id': '0101001','event_info.action': {'$in': [1, 4,6,9,16,18,21,23,24]} } },
  3.     { $group:{ _id:"$event_info.action",total:{ $sum:1} } } ]) ###分组求count数



  1. db.getCollection('redefine-collection').aggregate([
  2.     { $match:{ 'stime': {'$gte':1526486400000, '$lt':1526572800000},'event_id': '0101001','event_info.action': {'$in': [1, 4,6,9,16,18,21,23,24]} } },
  3.     { $group:{ _id:{action:"$event_info.action",uid:"$uid"},total:{ $sum:1} } } ]) ###多字段分组求count数



  db.getCollection('redefine-collection').aggregate([
{ $match:{ 'stime': {'$gte':1526486400000, '$lt':1526572800000},'event_id': '0101001','uid':{'$type':2},'event_info.action': {'$in': [1, 4,6,9,16,18,21,23,24]} } },
{ $group:{   _id:{action:"$event_info.action",uid:"$uid"},total:{ $sum:1}      } },
{ $group:{ _id:"$_id.action",total:{ $sum:"$total"} }}])
###分组后再分组,相当于子查询


####先分组,再过滤(group by  having)
db.zipcodes.aggregate( [ 
{ $group: { _id: "$state", totalPop: { $sum: "$pop" } } }, 
{ $match: { totalPop: { $gte: 10*1000*1000 } } } 
] )

python mongo如何使用聚合函数:
http://api.mongodb.com/python/current/examples/aggregation.html

参考:
https://www.cnblogs.com/sweetchildomine/p/6430368.html
http://www.mongoing.com/docs/core/aggregation-pipeline.html
https://blog.csdn.net/xsdxs/article/details/52700189               -----  mongodb 中max、min、sum、avg等函数用法

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29096438/viewspace-2154727/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29096438/viewspace-2154727/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值