Edit: Thanks to Mike Keen for asking about the reduce function. It was wrong in the rereduce case but I was just lazy and hadn't updated it. It should now work correctly in the rereduce case.
Basic statistics seem to be giving people new to Map/Reduce difficulty, including myself. Here's a short example on how to get the average value of a group of records. Suppose you have a number of records in the format: { 'value' : 345 }
The goal is to write a mapping function that emits a key pair with a non-unique key and a value representing the thing you want to average. The non-unique key is important because the values will be appended in the intermediate hash table in a list-style fashion, rather than being replaced like a normal hash table.
function(doc) { emit('average', doc.value); }
This mapping function makes a single key of "average" available to the reducing function. Since all the numerical values we want were emitted under the "average" key the reducing function only has to sum the values and divide by the length. In a more sophisticated case we could change the result to utilize the rereduce flag and allow incremental processing.
function(key,values,rereduce) { if(rereduce){ count = 0; sum = 0; for(i in values){ count += values[i]['count']; sum += values[i]['sum']; } return {"sum" : sum, "count" : count}; }else{ return {"sum" : sum(values), "count" : values.length}; } }