Mongodb的mapreduce如何实现只更新现有表的部分字段,而不是整个文档
给我你的怀抱
给我你的怀抱 2017-04-24 15:59:55
0
1
621

MapRedue 的输出

{"out", option }

option可以是下面几个选项:

  • "collection name" – mapReduce的输出结果会替换掉原来的collection,collection不存在则创建
  • { replace : "collection name" } – 同上
  • { merge : "collection name" } – 将新老数据进行合并,新的替换旧的,没有的添加进去
  • { reduce : "collection name" } – 存在老数据时,在原来基础上加新数据(即 new value = old value + mapReduce value)
  • { inline : 1 } – 不会创建collection,结果保存在内存里,只限于结果小于16MB的情况
    如果用collection name作option不能与其它option一起使用,其它则可以,如:

    • { "out", { replace : "collection name", db : "db name" } }

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有没有相应的解决方案。

给我你的怀抱
给我你的怀抱

reply all(1)
小葫芦

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template