感觉 mongodb-server (2.4.9-1ubuntu2)貌似聚合语句跟网上的版本不一样呢?
这样的{$project:{_id:1,s1:1,offer_id:1}}语句很少有意义啊在我这儿?!
group by s1
db.conversion_20170503.aggregate([{"$group":{"_id":"$s1","s1_counts":{"$sum":1},"offer_id":{"$first":"$offer_id"},}},{"$project":{"s1":"$_id","s1_counts":"$s1_counts","offer_id":1}}])
group by s1
db.conversion_20170503.aggregate([{"$group":{"_id":"$s1","s1_counts":{"$sum":1},"offer_id":{"$first":"$offer_id"},}},{"$project":{"_id":0,"s1":"$_id","offer_id":"$offer_id","s1_total":"$s1_counts"}}])
group by offer_id
db.conversion_20170503.aggregate([{"$group":{"_id":"$offer_id","s1_counts":{"$sum":1},"s1":{"$first":"$s1"},}},{"$project":{"_id":0,"s1":"$s1","offer_id":"$_id","s1_total":"$s1_counts"}}])
如何多次group ? good !
group by offer_id,s1
db.conversion_20170503.aggregate([{"$group":{"_id":{"offer_id":"$offer_id","s1":"$s1"},"s1_counts":{"$sum":1},"s1":{"$first":"$s1"},}},{"$project":{"_id":0,"s1":"$s1","groupby":"$_id","s1_total":"$s1_counts"}}])
group by n_id,s1
db.conversion_20170503.aggregate([{"$group":{"_id":{"n_id":"$n_id","s1":"$s1"},"s1_counts":{"$sum":1},"s1":{"$first":"$s1"},}},{"$project":{"_id":0,"s1":"$s1","groupby":"$_id","s1_total":"$s1_counts"}}])
极简版:
group by n_id,s1
db.conversion_20170503.aggregate([{$group : {_id : {"n_id":"$n_id","s1":"$s1"}, total : {$sum : 1}}}])
group by ip
db.conversion_20170503.aggregate([{$group : {_id : "$ip", total : {$sum : 1}}}])
group by n_id,s1 using fixed n_id :
db.click_20170503.aggregate([{$match:{n_id:25}},{$group : {_id : {"n_id":"$n_id","s1":"$s1"}, total : {$sum : 1}}}])