ITPub博客

首页 > 数据库 > NoSQL > 【Mongo】mongo聚合操作

【Mongo】mongo聚合操作

原创 NoSQL 作者:小亮520cl 时间:2018-05-19 13:15:28 0 删除 编辑
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博客 ” ,链接:https://blog.itpub.net/29096438/viewspace-2154727/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
毕业以后专业任职数据库工程师职位,itpub一直作为自己的笔记记录的地方,blog写的不详细,草书笔记,仅供参考!

注册时间:2013-09-12

  • 博文量
    529
  • 访问量
    1126857