Skip to content

Instantly share code, notes, and snippets.

@keepcosmos
Last active August 29, 2015 13:58
Show Gist options
  • Save keepcosmos/10344167 to your computer and use it in GitHub Desktop.
Save keepcosmos/10344167 to your computer and use it in GitHub Desktop.
mongodb mapreduce group by
db.messenger_rooms.mapReduce(
function(){
var date = new Date(this.created_at);
date.setHours(date.getHours());
var dateKey = (date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate());
var reserved_count = (this.reservation_status == undefined || this.reservation_status == null) ? 0 : 1;
emit(dateKey, {count: 1, reserved_count: reserved_count});
},
function(key, values){
var sum = {count: 0, reserved_count: 0}
values.forEach(function(value){
sum["count"] += value["count"];
sum["reserved_count"] += value["reserved_count"];
});
return sum;
},
{
query: {
created_at: {$gte: ISODate("2014-04-01"), $lte: ISODate("2014-04-03")}
},
out: "daily_rooms_count"
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment