MapReduceCommand.OutputType REDUCE 在 MongoDB 的 MapReduce 命令中做了什么

What does MapReduceCommand.OutputType REDUCE do in MapReduce command in MongoDB

我正在使用 collection.mapReduce(MapReduceCommand command) 进行 运行 MapReduce 作业。

MapReduceCommand 有一个 enum 输入 OutputType。可以是:

  1. 替换
  2. 合并
  3. 减少
  4. 内联

文档说:

public static final MapReduceCommand.OutputType MERGE

Merge the job output with the existing contents of outputTarget collection

public static final MapReduceCommand.OutputType REDUCE

Reduce the job output with the existing contents of outputTarget collection

OutputType.REDUCE 到底是做什么的?

因为没有人回答,所以我在 MongoDB-User Group

上问了这个问题

假设 cars 是您的 outputcollection(您要在其中存储 map-reduce 作业的输出)。

  • REPLACE:如果您当前的 MR 成功,则 cars 中的所有文档都将被删除(无论它们的 _id 是什么)并替换为当前结果。

  • MERGE: cars 中的文档不会被删除。相反,当前结果中的每个文档都将用相同的 _id 替换 cars 中已经存在的文档。如果 cars 中没有任何具有该 _id 的文档,它将被插入。您可以将其视为更新插入:

    db.cars.update({_id: newDocument._id}, {value: newDocument.value}, {upsert: true})
    
  • REDUCE:这与 MERGE 非常相似。但是,不仅仅是替换现有文档,这两个文档都将成为您的 reduce 函数(即 reduce([oldDocument, newDocument]))的输入,并且生成的文档将替换现有文档。

  • INLINE: Returns 您的结果作为一个变量,其方式与函数相同。 MongoDB 中没有存储任何内容,因此这不会影响任何集合。

可以找到完整的答案here