Aggregation Commands Comparison
The following table provides a brief overview of the features of theMongoDB aggregation commands.
aggregate / db.collection.aggregate() | mapReduce / db.collection.mapReduce() | |
---|---|---|
Description | Designed with specific goals of improving performance andusability for aggregation tasks.Uses a “pipeline” approach where objects are transformed as theypass through a series of pipeline operators such as$group , $match , and $sort .See Aggregation Pipeline Operators for more informationon 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 everyinput document.Can also generate new documents or filter out documents. | In addition to grouping operations, can perform complexaggregation tasks as well as perform incremental aggregation oncontinuously growing datasets.See Map-Reduce Examples andPerform Incremental Map-Reduce. |
Flexibility | Limited to the operators and expressions supported by theaggregation 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 asAggregation Pipeline Operators for more information on allthe available pipeline operators. | Custom map , reduce and finalize JavaScriptfunctions offer flexibility to aggregation logic.See mapReduce for details and restrictionson the functions. |
Output Results | Returns results as a cursor. If the pipeline includes the$out stage or $merge stage, the cursoris empty.Changed in version 3.6: MongoDB 3.6 removes the use of aggregate commandwithout the cursor option unless the command includes theexplain option. Unless you include the explain option, you mustspecify the cursor option.- To indicate a cursor with the default batch size, specify cursor:{} .- To indicate a cursor with a non-default batch size, use cursor: {batchSize: <num> } . | Returns results in various options (inline, new collection, merge,replace, reduce). See mapReduce for details on theoutput options. |
Sharding | Supports non-sharded and sharded input collections. | Supports non-sharded and sharded input collections. |
More Information | - Aggregation Pipeline- db.collection.aggregate() - aggregate | - Map-Reduce- db.collection.mapReduce() - mapReduce . |