How does Mongodb’s mapreduce implement updating only some fields of an existing table instead of the entire document

  mongodb, question

MapRedue output

{“out”, option }

Option can be the following options:

  • “collectionname”-the output of MapReduce will replace the original collection, and the collection will be created if it does not exist.
  • {replace: “collectionname”}–same as above
  • {merge: “collectionname”}–merge the new and old data, replace the old with the new, and add the ones without
  • {reduce: “collectionname”}–when there is old data, add new data to the original (i.e., newvalue = oldvalue+mapreducevalue)
  • {inline: 1}–collection will not be created and the results will be stored in memory, only if the results are less than 16MB.
    If collection name is used as option and cannot be used with other options, others can be used, such as:

    • { “out”, { replace : “collection name”, db : “db name” } }

The merge option is most likely, but experiments show that it always updates the entire document according to “_id” instead of only updating fields in reduce or finalize objects.

For example, I have an existing set A with more than 10 fields and no emps field. I run an MR operation and reduce to {key, emps: [[“0132”, 70], [“1443”, 30]]}. I want to add emps field to the existing set A. As a result, I have no more than 10 fields and only EMPS field.
Originally, I wanted to manually update db objects in reduce, but the current version does not allow db objects to be referenced.
Now only after mapreduce can a forEach be executed and update be executed manually.
Does mapreduce have a corresponding solution?

Usereduce, he will put you in the result setExisting resultsAndThe newly calculated resultsUse the reduce function (reducer provided to calculate it and save the final result. The most important thing here is to make the intermediate results output by mapper have the same format as the final results. Such a reducer is used in two places, one in reduce and the other in output. if they have the same format, especially hierarchy, it is easy to write a lot. Reducer can be understood as being only responsible for merging multiple results. If mapper outputs only one intermediate result, it should be able to directly serve as the final result without going through reducer. In fact, in MongoDB, when a key corresponds to only one mapper output, it is indeed output directly without 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.