Skip to content

Instantly share code, notes, and snippets.

@TylerBrock
Created January 13, 2012 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TylerBrock/1608755 to your computer and use it in GitHub Desktop.
Save TylerBrock/1608755 to your computer and use it in GitHub Desktop.
Map Reduce for Free-5947
db.logs.remove()
db.logs.insert(
{"name":"James Carr",
"log" : [
{
"comment" : "",
"time" : ISODate("2011-11-01T21:37:44.897Z"),
"user" : "Brad ************",
"action" : "Call",
"_id" : ObjectId("4eb066a875385d0a71063056")
},
{
"comment" : "vm em",
"time" : ISODate("2011-11-01T21:38:58.751Z"),
"user" : "Brad **********",
"action" : "Left Message",
"_id" : ObjectId("4eb066f275385d0a71063076")
},
{
"comment" : "Rest period expired, ready to be re-contacted.",
"time" : ISODate("2011-11-02T00:41:09.517Z"),
"user" : "",
"action" : "recycled",
"_id" : ObjectId("4eb091a587d45c5073015317")
}
],
} )
// map function
map = function(){
this.log.forEach(
function(log){
year = log.time.getFullYear()
month = log.time.getMonth()
day = log.time.getDate()
emit( {name: this.name, year: year, month: month, day: day} , { count : 1 } );
}
);
};
// reduce function
reduce = function(key, values) {
var count = 0;
values.forEach(function(v) {
count += v['count'];
});
return {count: count};
}
db.logs.mapReduce(map, reduce, {out: "log_results"});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment