search

Home  >  Q&A  >  body text

Mongodb的mapreduce如何实现只更新现有表的部分字段,而不是整个文档

MapRedue 的输出

{"out", option }

option可以是下面几个选项:

merge选项的可能性最大,但是经过实验发现,它总是会根据“_id”更新整个文档,而不是只更新reduce或者finalize出来的对象里有的字段。

比如我一个已经存在的集合A,有10几个字段,没有emps字段,我运行一个MR操作,reduce出来的是{key,emps:[["0132",70],["1443",30]]},想把emps字段添加到现有的集合A上,结果我10几个字段都没有了,就只剩下emps字段了。
本来想在reduce里引用db对象手工update的,但是现在的版本都不让引用db对象了。
现在只能在mapreduce后,执行一个forEach,手工执行update。
请问mapreduce有没有相应的解决方案。

给我你的怀抱给我你的怀抱2819 days ago643

reply all(1)I'll reply

  • 高洛峰

    高洛峰2017-04-24 16:01:55

    Use reduce, it will calculate your existing results and newly calculated results in the result set using the reduce function (reducer) you provided, and save the final result. The most important thing here is to make the intermediate results output by the mapper have the same format as the final results. Such a reducer is used in two places, once in reduce and once in output. If they have the same format, especially the hierarchy, it will be much easier to write. The reducer can be understood as being only responsible for merging multiple results. If the mapper only outputs an intermediate result, it should be directly used as the final result without going through the reducer. In fact, in MongoDB, when a key corresponds to only one mapper output, it is indeed output directly without going through the reducer.

    http://docs.mongodb.org/manual/reference/method/db.collection.mapReduce/#output-to-a-collection-with-an-action

    Merge the new result with the existing result if the output collection already exists. If an existing document has the same key as the new result, apply the reduce function to both the new and the existing documents and overwrite the existing document with the result.

    reply
    0
  • Cancelreply