Aggregation Commands Comparison聚合命令比较
On this page本页内容
Note
Aggregation Pipeline as Alternative to Map-Reduce聚合管道作为Map Reduce的替代方案
Starting in MongoDB 5.0, map-reduce is deprecated:从MongoDB 5.0开始,不赞成使用map-reduce:
Instead of map-reduce, you should use an aggregation pipeline.您应该使用聚合管道,而不是map-reduce。Aggregation pipelines provide better performance and usability than map-reduce.聚合管道提供了比映射减少更好的性能和可用性。You can rewrite map-reduce operations using aggregation pipeline stages, such as您可以使用聚合管道阶段(如$group
,$merge
, and others.$group
、$merge
和其他阶段)重写映射减少操作。For map-reduce operations that require custom functionality, you can use the对于需要自定义功能的map reduce操作,可以使用$accumulator
and$function
aggregation operators, available starting in version 4.4. You can use those operators to define custom aggregation expressions in JavaScript.$accumulator
和$function
聚合运算符,这些运算符从4.4版开始提供。您可以使用这些运算符在JavaScript中定义自定义聚合表达式。
For examples of aggregation pipeline alternatives to map-reduce, see:有关映射减少的聚合管道替代方案的示例,请参阅:
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() | |
---|---|---|
$group , $match , and $sort .$group 、$match 和$sort )时进行转换。 | ||
$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. $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 可以将结果(插入新文档、合并文档、替换文档、保留现有文档、操作失败、使用自定义更新管道处理文档)合并到现有集合中。
| ||
$accumulator and $function .$accumulator 和$function 定义自定义聚合表达式。$project pipeline operator.$project 管道运算符将子字段提取到结果的顶层。$project for more information as well as Aggregation Pipeline Operators for more information on all the available pipeline operators.$project ;有关所有可用管道运算符的详细信息,请参阅聚合管道运算符。
| map , reduce and finalize JavaScript functions offer flexibility to aggregation logic.map 、reduce 和finalize 确定JavaScript函数为聚合逻辑提供了灵活性。mapReduce for details and restrictions on the functions.mapReduce 。
| |
$out stage or $merge stage, the cursor is empty.$out 阶段或$merge 阶段,则游标为空。$out , you can replace an existing output collection completely or output to a new collection. See $out for details.$out ,您可以完全替换现有的输出集合,也可以输出到新的集合。有关详细信息,请参阅$out 。$merge , you can output to a new or existing collection. $merge ,您可以输出到新的或现有的集合。$merge for details.$merge 。
| mapReduce for details on the output options.mapReduce 。 | |
$merge | ||
Tip