1.mark 记个笔记
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"} }}])
###分组后再分组,相当于子查询
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等函数用法
- db.getCollection('redefine-collection').aggregate([
- { $match:{ 'stime': {'$gte':1526486400000, '$lt':1526572800000},'event_id': '0101001','event_info.action': {'$in': [1, 4,6,9,16,18,21,23,24]} } },
- { $group:{ _id:"$event_info.action",total:{ $sum:"$page_source"} } } ####分组求和
- ])
-
- db.getCollection('redefine-collection').aggregate([
- { $match:{ 'stime': {'$gte':1526486400000, '$lt':1526572800000},'event_id': '0101001','event_info.action': {'$in': [1, 4,6,9,16,18,21,23,24]} } },
- { $group:{ _id:"$event_info.action",total:{ $sum:1} } } ]) ###分组求count数
- db.getCollection('redefine-collection').aggregate([
- { $match:{ 'stime': {'$gte':1526486400000, '$lt':1526572800000},'event_id': '0101001','event_info.action': {'$in': [1, 4,6,9,16,18,21,23,24]} } },
- { $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 } } }
] )
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/