Skip to content

Instantly share code, notes, and snippets.

@keepcosmos
Created April 10, 2014 05:06
Show Gist options
  • Save keepcosmos/10344110 to your computer and use it in GitHub Desktop.
Save keepcosmos/10344110 to your computer and use it in GitHub Desktop.
mongo db group by count
db.messenger_rooms.group({
keyf: function(doc){
var date = new Date(doc.created_at);
date.setHours(date.getHours() + 9);
var dateKey = (date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate());
return {'day':dateKey};
},
cond: {created_at: {$gte: ISODate("2014-04-01"), $lte: ISODate("2014-04-03")}},
initial: { count: 0, reserved_count: 0},
reduce: function(doc, aggregator){
aggregator.count += 1;
if(doc.reservation_status !== undefined && doc.reservation_status !== null) aggregator.reserved_count++;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment