Skip to content

Instantly share code, notes, and snippets.

@RamaRoberts
Created July 3, 2013 19:54
Show Gist options
  • Save RamaRoberts/5922181 to your computer and use it in GitHub Desktop.
Save RamaRoberts/5922181 to your computer and use it in GitHub Desktop.
Runs a map/reduce across a MongoDB data set.
var end = new Date();
var start = new Date();
start.setTime(start.getTime()-300000); //5 minutes prior
// print dates in the format YYYY-MM-DD HH:MM:SS so we can do Mysql LOAD IN FILE on them
// Note that in JS the month portion of the Date constructor is 0-indexed
var my_s = start.getFullYear() + "-" + (start.getMonth()+1) + "-" + start.getDate() + " " + start.getHours() + ":" + start.getMinutes() + ":" + start.getSeconds();
var my_e = end.getFullYear() + "-" + (end.getMonth()+1) + "-" + end.getDate() + " " + end.getHours() + ":" + end.getMinutes() + ":" + end.getSeconds();
print(my_s + ',' + my_e);
map = function() {
emit({aid:this.aid, type:this.type}, {count: 1});
}
reduce = function (key, values) {
var count = 0;
values.forEach(function (v) {count += v.count;});
return {count:count};
}
db.realtime.mapReduce(map, reduce, {query : {ts: {$gte: start, $lt: end}}, out: "rtresults"});
var rtcount = db.rtresults.count();
print('Result count: ' + tojson(rtcount));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment