Docs Menu

Docs HomeView & Analyze DataAtlas Charts

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 Atlas 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. (Optional) Select Format to arrange the query as follows:

    • Split the query across multiple lines

    • Indent the query as per JSON conventions

  3. (Conditional) If you selected the Format option, click Close when you finish.

  4. 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
click to enlarge

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
click to enlarge

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

  • Charts doesn't support the $lookup operator in aggregation queries. 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 Create and Manage Charts Views.

  • Charts supports the $function operator only when you define the function body in a single line and wrap it in double quotes. To use this operator in your aggregation queries, you must use the following syntax:

    {
    $function: {
    body: "function(arg1, arg2, ...) { ... }",
    args: <array expression>,
    lang: "js"
    }
    }
←  Add a Lookup FieldSave and Reuse Queries →