db.runCommand(
{mapReduce: 'access',
map: function () {
emit({createDate: this.createDate},{cookie: this.cookie,ip: this.ip,session: this.session,standingTime: this.standingTime})
},
reduce: function (key, values) {
var keystr = JSON.stringify(key);
if(!uvMap[keystr]){uvMap[keystr]={};};
reduced = {pv:0,ip:0,uv:0,se:0,st:0};
values.forEach(function (val) {
if (val.cookie) {
reduced.pv += 1;
reduced.st += val.standingTime;
uvkey = val.cookie + '_' + val.ip;
if(!uvMap[keystr][uvkey]){uvMap[keystr][uvkey]=1;reduced.uv += 1; }
if(!ipMap[val.ip]){ipMap[val.ip]=1;reduced.ip += 1;}
if(!seMap[val.se]){seMap[val.se]=1;reduced.se += 1;}
} else {
reduced.pv += val.pv;
reduced.ip += val.ip;
reduced.uv += val.uv;
reduced.se += val.se;
reduced.st += val.st;
}
});
return reduced;
},
query: {"createDate":"2017-05-15"},
out: { merge : "post_man" },
scope: {uvMap: {},ipMap: {},seMap: {}},
finalize: function (key, val) {
if (!val.cookie) {return val;}
reduced = {pv: 1, se: 1, uv: 1,ip: 1, st:val.standingTime};
return reduced;
}
})
转载于:https://my.oschina.net/jj2050/blog/900584