Database Reference
In-Depth Information
},
{
"_id" : "yellow",
"value" : null
}
],
"timeMillis" : 95,
"counts" : {
"input" : 1000,
"emit" : 1000,
"reduce" : 55,
"output" : 11
},
"ok" : 1,
}
This shows that each “key” color value is split out individually and is the unique _id value for each document.
Because we specified nothing for the value portion of each document, that is set to null . We can modify this by adding
the output section for our desired MapReduce results. In this case we want a summary of what each functions takes.
To do that we can use the function to modify what we want to return as the object in place of null . In this case, let's
return the sum of all values for each of those colors. To do this we can create a function that will return the sum of all
the array of numbers for each color that's passed into the reduce function. Thankfully, we can use a handy function
called Array.sum to sum all values of an array. This gives us the following reduce function:
var reduce = function(color, numbers) {
return Array.sum(numbers);
};
Perfect. In addition to our inline output we can also have MapReduce write to a collection; to do so, we simply
have to replace that { inline : 1 } with the name of the collection we wish to output to. So let's output to a
collection called mrresult . This gives us the following command:
db.mapreduce.mapReduce(map,reduce,{ out: "mrresult" });
When executed with our new reduce function, it gives us the following:
{
"result" : "mrresult",
"timeMillis" : 111,
"counts" : {
"input" : 1000,
"emit" : 1000,
"reduce" : 55,
"output" : 11
},
"ok" : 1,
}
Search WWH ::




Custom Search