# Accumulators

## Group Accumulators

{% hint style="info" %}
These functions are available for use in the $group stage, accumulators are operators that maintain their state (e.g. totals, maximums, minimums, and related data) as information progress through the pipeline.
{% endhint %}

When used as accumulators in the $group stage, these operators take as input a single expression, evaluating the expression once for each input document, and maintain their stage for the group of documents that share the same group key.

For example:

| Variable    | Access via $   | Brief Description                                                                                     |
| ----------- | -------------- | ----------------------------------------------------------------------------------------------------- |
| accumulator | `$accumulator` | Returns the result of a user-defined accumulator function                                             |
| addToSet    | `$addToSet`    | Returns an array of unique expression values for each group. Order of the array elements is undefined |
| avg         | `$avg`         | Returns an average of numerical values. Ignores non-numeric values                                    |

## Accumulators

{% hint style="info" %}
Some operators that are available as accumulators for the $group stage are also available for use in other. When used in these other stages, these operators do not maintain their state and can take as input either a single argument or multiple arguments.
{% endhint %}

For example:

| Variable     | Access via $    | Brief Description                                                                                           |
| ------------ | --------------- | ----------------------------------------------------------------------------------------------------------- |
| addToSet     | `$addtoSet`     | Returns array of unique pushed values                                                                       |
| avg          | `$avg`          | Calculates the average of a number property or expression in the group.                                     |
| count        | `$count`        | Performs a count of a grouped documents or, if the property is an array, a count of the number of elements. |
| first        | `$first`        | Returns the first value from the group. The result depends on the sort field(s)                             |
| last         | `$last`         | Returns the last value from the group. The result depends on the sort field(s)                              |
| mergeObjects | `$mergeObjects` | Combines multiple documents into a single document                                                          |
| min          | `$min`          | Returns the smallest value from the group.                                                                  |
| max          | `$max`          | Returns the largest value from the group                                                                    |
| push         | `$push`         | Returns the array of pushed values                                                                          |
| sum          | `$sum`          | Calculates the sum of a number property or expression in the group                                          |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.medable.com/cortex-api/expressions/accumulators.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
