MongoDB MapReduce - Emit one key/one value doesnt call reduce

mapreduce, mongodb, pymongo

Solution

The reduce function combines documents with the same key into one document. If the map function emits a single document for a particular key (as is the case with key 3), the reduce function will not be called.

Problem

So i'm new with mongodb and mapreduce in general and came across this "quirk" (or atleast in my mind a quirk) Say I have objects in my collection like so: {'key':5, 'value':5} {'key':5, 'value':4} {'key':5, 'value':1} {'key':4, 'value':6} {'key':4, 'value':4} {'key':3, 'value':0} My map function simply emits the key and the value My reduce function simply adds the values AND before returning them adds 1 (I did this to check to see if the reduce function is even called) My results follow: {'_id': 3, 'value': 0} {'_id':4, 'value': 11.0} {'_id':5, 'value': 11.0} As you can see, for the keys 4 & 5 I get the expected answer of 11 BUT for the key 3 (with only one entry in the collection with that key) I get the unexpected 0! Is this natural behavior of mapreduce in general? For MongoDB? For pymongo (which I am using)?

Original source