After playing with mapreduce with mongoDB, I worry about the results having “two dimensions” instead of flat documents.
I.e. the documents look like {id:…, value:{test:123}}
Instead, I’d prefer this format: {…,test:123}
How to chose this style for the results/output collection?
Thanks
The output collection from a MongoDB map/reduce job will always have a top-level structure of
That is, it will always be a document with two keys. If you want to have the ‘value’ be a single value, you can do so by controlling your emit() and reduce() functions.
Here is an emit() function that will emit a single value for ‘value’:
Here is a reduce() function that will return a single value for ‘value’:
Remember: the “shape” of the document emitted by emit() MUST be the same as the one returned by reduce().
If you do this, the documents in the output collection will look something like this: