Skip to content

Instantly share code, notes, and snippets.

@lakshmankashyap
Forked from cschlyter/mongo_group_by_day.js
Created August 17, 2020 13:32
Show Gist options
  • Save lakshmankashyap/2b9d85c07d03097a7a8cfd01c730d2c8 to your computer and use it in GitHub Desktop.
Save lakshmankashyap/2b9d85c07d03097a7a8cfd01c730d2c8 to your computer and use it in GitHub Desktop.
MongoDB: group data by day (group)
db.link_access.group(
{
keyf: function(doc) {
var date = new Date(doc.date);
var dateKey = (date.getMonth()+1)+"/"+date.getDate()+"/"+date.getFullYear()+'';
return {'day': dateKey};
},
cond: {short_id: "N"},
initial: {count:0},
reduce: function(obj, prev) {prev.count++;}
});
//http://stackoverflow.com/questions/5168904/group-by-dates-in-mongodb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment