【Mongo】mongo聚合操作

小亮520cl發表於2018-05-19
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

https://blog.csdn.net/xsdxs/article/details/52700189               ----- mongodb 中max、min、sum、avg等函式用法

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29096438/viewspace-2154727/,如需轉載,請註明出處,否則將追究法律責任。

相關文章