> This page location: /docs.md > /docs/development.md > /docs/charts.md > /docs/charts/build-charts.md > /docs/charts/reshape-data.md

> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# Run Aggregation Pipelines on Your Data

[Aggregation pipelines](https://www.mongodb.com/docs/manual/core/aggregation-pipeline.md#std-label-aggregation-pipeline) 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.

## Example

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:

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

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

1. The [`$unwind`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/unwind.md#mongodb-pipeline-pipe.-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`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/addFields.md#mongodb-pipeline-pipe.-addFields) stage adds a new field to the documents called `saleAmount`. The [`$multiply`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/multiply.md#mongodb-expression-exp.-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](/images/charts/query-bar-agg-example.png)

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

## Address Aggregation Pipeline Errors

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 (MongoDB Query Language) or unsupported pipeline stages.

![Example Aggregation Pipeline Error](/images/charts/agg-pipeline-error.png)

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

## Limitations

- Charts doesn't support the [`$lookup`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/lookup.md#mongodb-pipeline-pipe.-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.](https://www.mongodb.com/docs/charts/create-manage-charts-view.md#std-label-data-sources-pipeline)

- Charts supports the [`$function`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/function.md#mongodb-expression-exp.-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:

  ```javascript
  {
    $function: {
      body: "function(arg1, arg2, ...) { ... }",
      args: <array expression>,
      lang: "js"
    }
  }
  ```
