如何更改MongoDB的map-reduce结果的结构? [英] How to change the structure of MongoDB's map-reduce results?

查看:83
本文介绍了如何更改MongoDB的map-reduce结果的结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在Mongo数据库上运行Map-Reduce时,通常会得到类似于以下内容的结果:

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>, ... } }

是否可以省略value: { ... }部分,直接在结果中插入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.

我还有一个关于Map-Reduce的问题:是否可以通过mapreduce函数访问另一个集合?

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

推荐答案

MapReduce仅返回{_id:some_id,value:some_value}形式的文档

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

"some_value"不一定必须是嵌入式文档,但是在大多数情况下,它允许Map Reduce函数计算多个变量. Reduce函数返回的文档必须采用与输入形式相同的格式,因为对于任何给定的_id值,Reduce函数都可以重复运行.

"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.

要逐步了解Map Reduce的工作原理,请参阅MongoDB Cookbook食谱的其他"部分,标题为使用版本化文档查找最大和最小值".

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}

可以执行增量Map Reduce,它将合并多个Map Reduce函数的结果. http://www.mongodb.org/display/DOCS/MapReduce#MapReduce-IncrementalMapreduce

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

最后,当前无法使用Map Reduce一次访问多个集合.有一项功能要求提供此功能,但尚未计划将其添加到任何即将发布的版本中.
https://jira.mongodb.org/browse/SERVER-970

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

这篇关于如何更改MongoDB的map-reduce结果的结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆