So I’m mapreducing using mongo, and sometimes, but not always, the first element of my reduce array is a bson object with another reduce array inside of it. The way I dealt with it was combining that array with the outer array results.
I don’t get why it’s happening, and can’t find any documentation on it. Can anyone point me in the right direction so I can be sure I’m dealing with this properly?
http://www.mongodb.org/display/DOCS/MapReduce#MapReduce-ReduceFunction has your answer (but you have to read carefully). See around the quoted section:
“Notice that the result document has the same structure as the documents emitted by the map function. This is important because, when the reduce function is run against a given key, it’s not guaranteed to process every single value for that key (or username). In fact, the reduce function may have to run more than once.”
Basically, reduce doesn’t run “all at once” on each “key” but may be invoked “incrementally” to process new results with already processed results.