Docs Menu

Run Aggregation Pipelines on Your Data

On this page

  • Example
  • Address Aggregation Pipeline Errors
  • Limitations

Aggregation pipelines transform your documents into an aggregated set of results. In MongoDB Charts, aggregation pipelines are commonly used to visualize new fields created from calculated results of pre-existing fields, but also have many other applications.

To create an aggregation pipeline:

  1. In the Query bar, input an aggregation pipeline. Your pipeline must be in square brackets.
  2. Click Apply to execute your pipeline.

The following chart shows total sale amounts from an office supply company, categorized by store location. The chart uses the following aggregation pipeline in the Query bar:

[
{
$unwind: "$items"
},
{
$addFields: {
saleAmount: {
$multiply: [ "$items.price", "$items.quantity" ]
}
}
}
]

This aggregation pipeline processes the collection data using the following order:

  1. The $unwind stage unwinds the items array and outputs a new document for each item in the array. Each element in the items array contains a single item sold during a transaction.
  2. The $addFields stage adds a new field to the documents called saleAmount. The $multiply expression sets the value of saleAmount to the product of items.price and items.quantity. You can see this new field highlighted in the following screenshot:
Example Aggregation Pipeline

Once the data has been processed using the pipeline, the chart displays the Sum of all saleAmounts categorized by store location.

If your aggregation is invalid, Charts displays the icon in the Query bar.

Click the Query bar if it is not already displayed to view error details. Charts displays error details for:

  • Client-side errors, such as malformed JSON, and
  • Server-side errors, such as invalid MQL or unsupported pipeline stages.
Example Aggregation Pipeline Error

Review the error details, then adjust your aggregation pipeline accordingly.

The $lookup operator is not supported in aggregation queries defined in the Chart Builder. However, you can use this operator in Data Source pipelines. To learn more about how to use pipelines to pre-process data before it reaches the Chart Builder, see Data Source Pipelines.

Give Feedback
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.