Aggregation Commands Comparison聚合命令比较

On this page本页内容

Tip

Starting in version 4.4, MongoDB adds the $accumulator and $function aggregation operators. 从版本4.4开始,MongoDB添加了$accumulator$function聚合运算符。Using $accumulator and $function , mapReduce expressions can be re-written using the aggregation operators.使用$accumulator$function,可以使用聚合运算符重新编写mapReduce表达式。

Even before version 4.4, some map-reduce expressions could also be rewritten using other aggregation pipeline operators, such as $group, $merge, etc.甚至在版本4.4之前,一些map reduce表达式也可以使用其他聚合管道运算符重写,例如$group$merge等。

Aggregation Commands Comparison Table聚合命令比较表

The following table provides a brief overview of the features of the MongoDB aggregation commands.下表简要概述了MongoDB聚合命令的功能。

 aggregate / db.collection.aggregate()mapReduce / db.collection.mapReduce()
Description描述

Designed with specific goals of improving performance and usability for aggregation tasks.设计目标是提高聚合任务的性能和可用性。

Uses a “pipeline” approach where objects are transformed as they pass through a series of pipeline operators such as $group, $match, and $sort.使用“管道”方法,对象在通过一系列管道运算符(如$group$match$sort)时进行转换。

See Aggregation Pipeline Operators for more information on the pipeline operators.有关管道运算符的更多信息,请参阅聚合管道运算符

Implements the Map-Reduce aggregation for processing large data sets.实现用于处理大型数据集的Map-Reduce聚合。
Key Features主要特征

Pipeline operators can be repeated as needed.管道运算符可以根据需要重复操作。

Pipeline operators need not produce one output document for every input document.管道运算符不需要为每个输入文档生成一个输出文档。

Can also generate new documents or filter out documents.还可以生成新文档或筛选出文档。

With the addition of $merge in version 4.2, can create on-demand materialized views, where the content of the output collection can be updated incrementally the pipeline is run. 在版本4.2中添加$merge后,可以创建按需物化视图,其中输出集合的内容可以在管道运行时增量更新。$merge can incorporate results (insert new documents, merge documents, replace documents, keep existing documents, fail the operation, process documents with a custom update pipeline) into an existing collection.$merge可以将结果(插入新文档、合并文档、替换文档、保留现有文档、操作失败、使用自定义更新管道处理文档)合并到现有集合中。

In addition to grouping operations, can perform complex aggregation tasks as well as perform incremental aggregation on continuously growing datasets.除了分组操作外,还可以执行复杂的聚合任务,以及对不断增长的数据集执行增量聚合。

See Map-Reduce Examples and Perform Incremental Map-Reduce.请参见Map-Reduce示例并执行增量地图缩小。

Flexibility灵活性

Starting in version 4.4, can define custom aggregation expressions with $accumulator and $function.从版本4.4开始,可以使用$accumulator$function定义自定义聚合表达式。

In previous versions, can only use operators and expressions supported by the aggregation pipeline.在以前的版本中,只能使用聚合管道支持的运算符和表达式。

However, can add computed fields, create new virtual sub-objects, and extract sub-fields into the top-level of results by using the $project pipeline operator.但是,可以使用$project管道运算符添加计算字段、创建新的虚拟子对象,并将子字段提取到顶级结果中。

See $project for more information as well as Aggregation Pipeline Operators for more information on all the available pipeline operators.有关更多信息,请参阅$project;有关所有可用管道运算符的更多信息,请参见聚合管道运算符

Custom map, reduce and finalize JavaScript functions offer flexibility to aggregation logic.自定义mapreducefinalize确定JavaScript函数为聚合逻辑提供了灵活性。

See mapReduce for details and restrictions on the functions.有关函数的详细信息和限制,请参阅mapReduce

Output Results输出结果

Returns results as a cursor. 以游标形式返回结果。If the pipeline includes the $out stage or $merge stage, the cursor is empty.如果管道包含$out阶段或$merge阶段,则光标为空。

With $out, you can replace an existing output collection completely or output to a new collection. 使用$out,可以完全替换现有的输出集合,也可以将输出替换为新集合。See $out for details.有关详细信息,请参阅$out

With $merge, you can output to a new or existing collection. 使用$merge,可以输出到新集合或现有集合。For existing cllections, you can specify how to incorporate the results into the output collection (insert new documents, merge documents, replace documents, keep existing documents, fail the operation, process documents with a custom update pipeline). 对于现有选择,可以指定如何将结果合并到输出集合中(插入新文档、合并文档、替换文档、保留现有文档、操作失败、使用自定义更新管道处理文档)。See $merge for details.有关详细信息,请参阅$merge

Returns results in various options (inline, new collection, merge, replace, reduce). 以各种选项(内联、新集合、合并、替换、减少)返回结果。See mapReduce for details on the output options.有关输出选项的详细信息,请参阅mapReduce
Sharding分片

Supports non-sharded and sharded input collections.支持非分片和分片输入集合。

$merge can output to a non-sharded or sharded collection.$merge可以输出到非分片或分片集合。

Supports non-sharded and sharded input collections.支持非分片和分片输入集合。
More Information更多信息