Skip to content

Instantly share code, notes, and snippets.

@rhyskeepence
Created September 5, 2012 11:32
Show Gist options
  • Save rhyskeepence/3635341 to your computer and use it in GitHub Desktop.
Save rhyskeepence/3635341 to your computer and use it in GitHub Desktop.
group by day via map reduce
var m = function() {
emit(this._id - (this._id % 86400000), { aggregate:0, count:1, sum: this['threadCount'] } );
};
var r = function (name, values) {
var result = { aggregate:0, count:0, sum:0 };
values.forEach(function(f) {
result.sum += f.sum;
result.count += f.count;
});
return result;
};
var f = function (who, res) {
if (res.count > 0) res.aggregate = res.sum / res.count;
return res;
};
db.prod.mapReduce(m, r, {finalize: f, query: { "_id": { $gte: someTimestamp }}, out: { inline : 1 }});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment