Skip to content

Instantly share code, notes, and snippets.

@gsamat
Created February 12, 2013 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsamat/4761441 to your computer and use it in GitHub Desktop.
Save gsamat/4761441 to your computer and use it in GitHub Desktop.
Find out all reading per hour in Bookmates mongo activity database using map-reduce. Old mongo, no aggregates :(
conn = new Mongo();
db = conn.getDB("publisher_bookmate");
db.getMongo().setSlaveOk();
var mapFunction1 = function() {
emit(this.read_at, this.size);
};
var reduceFunction1 = function(keyCustId, readcount) {
return Array.sum(readcount);
};
res= db.getCollection("stats.reading_hourlies").mapReduce(
mapFunction1,
reduceFunction1,
{
out: { inline: 1 },
query: {
"read_at": { $gte: ISODate("2013-02-11T00:00:00Z")}
}
}
)
//printjson(res)
var walk = function(o){
for(var prop in o){
if(o.hasOwnProperty(prop)){
var val = o[prop];
if (prop=="_id" || prop=="value") { print(val); }
if(typeof val == 'object'){
walk(val);
}
}
}
};
walk(res.results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment