The aggregation framework is MongoDB’s pipeline-based system for processing document data. Multistage pipelines filter, group, transform, calculate, and reshape records, passing each stage’s results to the next. section 1.3.1 section 6.0
Common pipeline capabilities include:
- Filter documents with $match, then group them with $group for sums, averages, counts, or other calculations. section 6.6
- Organize results with $sort and restrict processing with $limit. section 6.6
- Reshape documents with $set, $addFields, $project, or $unset; use $unwind to process array elements separately. section 6.6
- Combine related records with $lookup, or combine result sets with $unionWith. section 6.1
- Support reporting, dashboards, data discovery, ELT transformations, real-time analytics, and advanced text or vector search. section 6.1
A reporting pipeline
A report can pass documents through $match to select relevant records, $group to calculate totals, $sort to organize the results, and $limit to restrict the returned set. Breaking the transformation into stages makes it easier to understand, maintain, and debug. section 6.0
[ { <stage> }, { <stage> }, { <stage> }, { <stage> }, ... ]
Use the pipeline skeleton to visualize the ordered sequence of aggregation stages. section 6.1
Table 6.1 MongoDB operators for common SQL operations
| SQL terms, functions, and concepts |
MongoDB aggregation operators |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Use the SQL comparison to map familiar operations such as WHERE, GROUP BY, JOIN, ORDER BY, and UNION ALL to MongoDB operators. section 6.1
