Skip to content

Instantly share code, notes, and snippets.

@phnessu4
Last active April 18, 2018 08:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phnessu4/5636642 to your computer and use it in GitHub Desktop.
Save phnessu4/5636642 to your computer and use it in GitHub Desktop.
mongodb group by day
/**
* group by day
* @link https://gist.github.com/phnessu4/5636642
* @param query document {key1:123,key2:456}
*/
var count_by_day = function(query){
return db.action.group(
{
keyf: function(doc) {
var date = new Date(doc.time);
var dateKey = (date.getMonth()+1)+"/"+date.getDate()+"/"+date.getFullYear();
return {'date': dateKey};
},
cond:query,
initial: {count:0},
reduce: function(obj, prev) {
prev.count++;
}
});
}
count_by_day({this:'is',the:'query'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment