Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

mapreduce - How to change the structure of MongoDB's map-reduce results?

When I'm running Map-Reduce on a Mongo database, I usually get results similar to the following:

{ _id: <some-id>, value: { <first-key>: <first-value>, ... } }

Is there a way to omit the value: { ... } part and directly insert the content of value in the result? Basically, I'd like to have a result that looks like the following:

{ _id: <some-id>, <first-key>: <first-value>, ... }

This way I could merge the results back into an existing collection that obeys this format.

I also have an other question regarding Map-Reduce: Is it possible to access another collection from withing a map or reduce function?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

MapReduce only returns documents of the form {_id:some_id, value:some_value}

"some_value" does not necessarily have to be an embedded document, but in most cases it is to allow multiple variables to be calculated by the Map Reduce function. The documents returned by the Reduce function must be in the same form as they are input, because the Reduce function may be run repeatedly for any given _id value.

For a step-by-step of how Map Reduce works, please see the "Extras" section of the MongoDB Cookbook recipe titled "Finding Max And Min Values with Versioned Documents" http://cookbook.mongodb.org/patterns/finding_max_and_min/ This should provide a better understanding of how Map Reduce works, and why the output must be in the format {_id:some_id, value:some_value}

It is possible to do an incremental Map Reduce, which will merge the results of multiple Map Reduce functions. http://www.mongodb.org/display/DOCS/MapReduce#MapReduce-IncrementalMapreduce

Finally, it is currently not possible to access multiple collections at once with Map Reduce. There is a feature request for this capability, but it is not scheduled to be added to any upcoming versions.
https://jira.mongodb.org/browse/SERVER-970


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...