Aggregation
Overview
In this guide, you can learn how to use the MongoDB Node.js Driver to perform aggregation operations.
Aggregation operations process data in your MongoDB collections and return computed results. The MongoDB Aggregation framework is modeled on the concept of data processing pipelines. Documents enter a pipeline comprised of one or more stages, and this pipeline transforms the documents into an aggregated result.
To learn more about the aggregation stages supported by the Node.js Driver, see Aggregation Stages.
Analogy
The aggregation pipeline is similar to an automobile factory assembly line. An assembly lines has stations with specialized tools that are used to perform specific tasks. For example, when building a car, the assembly line begins with a frame. As the car frame moves though the assembly line, each station adds a new part. The factory transforms and assembles the initial parts, resulting in finished cars.
The aggregation pipeline is the assembly line, the aggregation stages are the assembly stations, and the expression operators are the specialized tools.
Compare Aggregation and Find Operations
The following table lists the different tasks you can perform with find operations compared to what you can achieve with aggregation operations. The aggregation framework provides expanded functionality that allows you to transform and manipulate your data.
Find Operations | Aggregation Operations |
---|---|
Select certain documents to return Select which fields to return Sort the results Limit the results Count the results | Select certain documents to return Select which fields to return Sort the results Limit the results Count the results Group the results Rename fields Compute new fields Summarize data Connect and merge data sets |
Server Limitations
Consider the following limitations when performing aggregation operations:
Returned documents must not violate the BSON document size limit of 16 megabytes.
Pipeline stages have a memory limit of 100 megabytes by default. If required, you can exceed this limit by enabling the AllowDiskUse property of the
AggregateOptions
object that you pass to theAggregate()
method.
Additional information
To view a full list of expression operators, see Aggregation Operators.
To learn about explaining MongoDB aggregation operations, see Explain Results and Query Plans.