mongodb快速聚合

1.  统计转化中的指定活动的ip出现次数:

var match ={"$match":{"_id" : /^3380.*$/}};

var group= {$group : {_id : {"campaign_id":"$campaign_id","ip":"$ip"},"ip_count":{$sum : 1},"ipset":{$addToSet : "$ip"}}};

mclog@mc-arch-mongo-ems-172-17-0-6> db.conversion_20170906.aggregate([match,group])

{ "_id" : { "campaign_id" : "3380", "ip" : "211.103.82.151" }, "ip_count" : 1, "ipset" : [ "211.103.82.151" ] }

{ "_id" : { "campaign_id" : "3380", "ip" : "106.39.191.44" }, "ip_count" : 1, "ipset" : [ "106.39.191.44" ] }

{ "_id" : { "campaign_id" : "3380", "ip" : "106.39.191.65" }, "ip_count" : 1, "ipset" : [ "106.39.191.65" ] }

{ "_id" : { "campaign_id" : "3380", "ip" : "14.116.141.129" }, "ip_count" : 1, "ipset" : [ "14.116.141.129" ] }

{ "_id" : { "campaign_id" : "3380", "ip" : "106.39.189.241" }, "ip_count" : 1, "ipset" : [ "106.39.189.241" ] }

{ "_id" : { "campaign_id" : "3380", "ip" : "223.104.94.26" }, "ip_count" : 1, "ipset" : [ "223.104.94.26" ] }

{ "_id" : { "campaign_id" : "3380", "ip" : "14.116.142.199" }, "ip_count" : 1, "ipset" : [ "14.116.142.199" ] }

{ "_id" : { "campaign_id" : "3380", "ip" : "116.226.119.56" }, "ip_count" : 1, "ipset" : [ "116.226.119.56" ] }

{ "_id" : { "campaign_id" : "3380", "ip" : "14.152.68.164" }, "ip_count" : 1, "ipset" : [ "14.152.68.164" ] }

{ "_id" : { "campaign_id" : "3380", "ip" : "14.215.160.238" }, "ip_count" : 1, "ipset" : [ "14.215.160.238" ] }

2. 统计转化中出现的所有ip (问题,此处的couont是记录数并不是ip真正出现的数量,怎么解决?ip!=""?):

var group= {$group : {_id : "$campaign_id","ip_count":{$sum : 1},"ipset":{$addToSet : "$ip"}}};
{ "_id" : "3380", "ip_count" : 28, "ipset" : [ "14.116.141.129", "14.116.142.199", "106.39.191.65", "14.116.142.83", "218.18.147.233", "59.54.47.246", "14.215

.160.152", "14.215.160.93", "116.226.19.165", "106.39.189.241", "119.39.18.25", "106.39.191.44", "223.104.94.26", "14.152.69.176", "14.116.141.63", "223.104.2

4.115", "14.152.69.107", "14.215.160.238", "211.103.82.151", "124.239.176.61", "175.188.164.228", "175.188.162.90", "223.104.10.29", "14.152.69.84", "116.226.

119.56", "117.136.45.152", "211.103.82.178", "14.152.68.164" ] }

3.  统计测试活动的点击数和ip

var match ={"$match":{"_id" : /^3806.*$/}};
var group= {$group : {_id : "$campaign_id","ip_count":{$sum : 1},"ipset":{$addToSet : "$ip"}}};

db.click_20170906.aggregate([match,group])

{ "_id" : 3806, "ip_count" : 3, "ipset" : [ "119.57.32.69" ] }



你可能感兴趣的:(mongodb快速聚合)