Last active
August 29, 2015 13:58
-
-
Save keepcosmos/10344167 to your computer and use it in GitHub Desktop.
mongodb mapreduce group by
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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