MongoDB中复杂的分组查询统计(MapReduce)

按日期、行为、用户统计行为表
由于行为表是分片表,不能使用group,只能采用MapReduce进行分组统计:
db.runCommand({
mapreduce:"pmhuseraction",
map:function() { emit({date:this.statustime.getFullYear() + "-" +this.statustime.getMonth() + "-" +this.statustime.getDate(),writetype:this.writetype,userid:this.userid}, 1); },
reduce:function (key, values) { var x = 0; values.forEach(function (v) {x += v;}); return x; },
out:"pmhuseraction_stat_tmp",
finalize:function (key, value) {return {date:key.date,writetype:key.writetype, userid:key.userid, count:value}}

})

发布了28 篇原创文章 · 获赞 19 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/fengyily/article/details/7659932