mapreduce - Map-Reduce count number of documents in each minute MongoDB -
i have mongodb collection has created_at
stored in each document. these stored mongodb date object e.g.
{ "_id" : "4cacda7eed607e095201df00", "created_at" : "wed oct 06 2010 21:22:23 gmt+0100 (bst)", text: "something" } { "_id" : "4cacdf31ed607e0952031b70", "created_at" : "wed oct 06 2010 21:23:42 gmt+0100 (bst)", text: "something" } ....
i count number of items created between each minute, can pass data google charts generate this:
how do map reduce function, or there fancy mongodb aggregate function use instead?
map function should emit timestamp object, adjusted minute, , count of 1. reduce function should sum counts:
map = function() { var created_at_minute = new date(this.created_at.getfullyear(), this.created_at.getmonth(), this.created_at.getdate(), this.created_at.gethours(), this.created_at.getminutes()); emit(created_at_minute, {count: 1}); } reduce = function(key, values) { var total = 0; for(var = 0; < values.length; i++) { total += values[i].count; } return {count: total}; }
Comments
Post a Comment