Aggregation Commands Comparison¶
The following table provides a brief overview of the features of the MongoDB aggregation commands.
aggregate | 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. See Aggregation Pipeline Operators for more information on the pipeline operators. |
Implements the Map-Reduce aggregation for processing large data sets. |
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. |
In addition to grouping operations, can perform complex aggregation tasks as well as perform incremental aggregation on continuously growing datasets. See Map-Reduce 例子 and 对增量数据做Map-Reduce. |
Flexibility | Limited to the 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. See $project for more information as well as Aggregation Pipeline Operators for more information on all the available pipeline operators. |
Custom map, reduce and finalize JavaScript functions offer flexibility to aggregation logic. See mapReduce for details and restrictions on the functions. |
Output Results | Returns results as a cursor. If the pipeline includes the $out stage, the cursor is empty. 在 3.4 版更改: MongoDB 3.4 deprecates the use of aggregate command without the cursor option, unless the pipeline includes the explain option. When returning aggregation results inline using the aggregate command, specify the cursor option using the default batch size cursor: {} or specify the batch size in the cursor option cursor: { batchSize: <num> }. |
Returns results in various options (inline, new collection, merge, replace, reduce). See mapReduce for details on the output options. |
Sharding | Supports non-sharded and sharded input collections. | Supports non-sharded and sharded input collections. |
Notes | Prior to 2.4, JavaScript code executed in a single thread. | |
More Information | See 聚合管道 and aggregate. | See 映射化简 and mapReduce. |