> Documentation index: https://firebase.google.com/docs/reference/llms.txt

# AggregateFunction


```
class AggregateFunction
```

<br />

*** ** * ** ***

A class that represents an aggregate function.

## Summary

| ### Public companion functions |
|---|---|
| `AggregateFunction` | `arrayAgg(expression: Expression)` Creates an aggregation that collects all values of an expression across multiple stage inputs into an array. |
| `AggregateFunction` | `arrayAgg(fieldName: String)` Creates an aggregation that collects all values of a field across multiple stage inputs into an array. |
| `AggregateFunction` | `arrayAggDistinct(expression: Expression)` Creates an aggregation that collects all distinct values of an expression across multiple stage inputs into an array. |
| `AggregateFunction` | `arrayAggDistinct(fieldName: String)` Creates an aggregation that collects all distinct values of a field across multiple stage inputs into an array. |
| `AggregateFunction` | `average(expression: Expression)` Creates an aggregation that calculates the average (mean) of values from an expression across multiple stage inputs. |
| `AggregateFunction` | `average(fieldName: String)` Creates an aggregation that calculates the average (mean) of a field's values across multiple stage inputs. |
| `AggregateFunction` | `count(expression: Expression)` Creates an aggregation that counts the number of stage inputs with valid evaluations of the provided `expression`. |
| `AggregateFunction` | `count(fieldName: String)` Creates an aggregation that counts the number of stage inputs where the input field exists. |
| `AggregateFunction` | `countAll()` Creates an aggregation that counts the total number of stage inputs. |
| `AggregateFunction` | `countDistinct(expression: Expression)` Creates an aggregation that counts the number of distinct values of an expression across multiple stage inputs. |
| `AggregateFunction` | `countDistinct(fieldName: String)` Creates an aggregation that counts the number of distinct values of a field across multiple stage inputs. |
| `AggregateFunction` | `countIf(condition: BooleanExpression)` Creates an aggregation that counts the number of stage inputs where the provided boolean expression evaluates to true. |
| `AggregateFunction` | `first(expression: Expression)` Creates an aggregation that finds the first value of an expression across multiple stage inputs. |
| `AggregateFunction` | `first(fieldName: String)` Creates an aggregation that finds the first value of a field across multiple stage inputs. |
| `AggregateFunction` | `last(expression: Expression)` Creates an aggregation that finds the last value of an expression across multiple stage inputs. |
| `AggregateFunction` | `last(fieldName: String)` Creates an aggregation that finds the last value of a field across multiple stage inputs. |
| `AggregateFunction` | `maximum(expression: Expression)` Creates an aggregation that finds the maximum value of an expression across multiple stage inputs. |
| `AggregateFunction` | `maximum(fieldName: String)` Creates an aggregation that finds the maximum value of a field across multiple stage inputs. |
| `AggregateFunction` | `minimum(expression: Expression)` Creates an aggregation that finds the minimum value of an expression across multiple stage inputs. |
| `AggregateFunction` | `minimum(fieldName: String)` Creates an aggregation that finds the minimum value of a field across multiple stage inputs. |
| `AggregateFunction` | `rawAggregate(name: String, vararg expr: Expression)` Creates a raw aggregation function. |
| `AggregateFunction` | `sum(expression: Expression)` Creates an aggregation that calculates the sum of values from an expression across multiple stage inputs. |
| `AggregateFunction` | `sum(fieldName: String)` Creates an aggregation that calculates the sum of a field's values across multiple stage inputs. |

| ### Public functions |
|---|---|
| `AliasedAggregate` | `alias(alias: String)` Assigns an alias to this aggregate. |
| `WindowFunction` | `@Beta over(window: WindowSpec)` Applies a window frame to this aggregate, turning it into a window function. |

## Public companion functions

### arrayAgg

```
fun arrayAgg(expression: Expression): AggregateFunction
```

Creates an aggregation that collects all values of an expression across multiple stage inputs into an array.

If the expression resolves to an absent value, it is converted to `null`. The order of elements in the output array is not stable and shouldn't be relied upon.

| Parameters |
|---|---|
| `expression: Expression` | The expression to collect values from. |

| Returns |
|---|---|
| `AggregateFunction` | A new `AggregateFunction` representing the array_agg aggregation. |

### arrayAgg

```
fun arrayAgg(fieldName: String): AggregateFunction
```

Creates an aggregation that collects all values of a field across multiple stage inputs into an array.

If the expression resolves to an absent value, it is converted to `null`. The order of elements in the output array is not stable and shouldn't be relied upon.

| Parameters |
|---|---|
| `fieldName: String` | The name of the field to collect values from. |

| Returns |
|---|---|
| `AggregateFunction` | A new `AggregateFunction` representing the array_agg aggregation. |

### arrayAggDistinct

```
fun arrayAggDistinct(expression: Expression): AggregateFunction
```

Creates an aggregation that collects all distinct values of an expression across multiple stage inputs into an array.

If the expression resolves to an absent value, it is converted to `null`. The order of elements in the output array is not stable and shouldn't be relied upon.

| Parameters |
|---|---|
| `expression: Expression` | The expression to collect values from. |

| Returns |
|---|---|
| `AggregateFunction` | A new `AggregateFunction` representing the array_agg_distinct aggregation. |

### arrayAggDistinct

```
fun arrayAggDistinct(fieldName: String): AggregateFunction
```

Creates an aggregation that collects all distinct values of a field across multiple stage inputs into an array.

If the expression resolves to an absent value, it is converted to `null`. The order of elements in the output array is not stable and shouldn't be relied upon.

| Parameters |
|---|---|
| `fieldName: String` | The name of the field to collect values from. |

| Returns |
|---|---|
| `AggregateFunction` | A new `AggregateFunction` representing the array_agg_distinct aggregation. |

### average

```
fun average(expression: Expression): AggregateFunction
```

Creates an aggregation that calculates the average (mean) of values from an expression across multiple stage inputs.

| Parameters |
|---|---|
| `expression: Expression` | The expression representing the values to average. |

| Returns |
|---|---|
| `AggregateFunction` | A new `AggregateFunction` representing the average aggregation. |

### average

```
fun average(fieldName: String): AggregateFunction
```

Creates an aggregation that calculates the average (mean) of a field's values across multiple stage inputs.

| Parameters |
|---|---|
| `fieldName: String` | The name of the field containing numeric values to average. |

| Returns |
|---|---|
| `AggregateFunction` | A new `AggregateFunction` representing the average aggregation. |

### count

```
fun count(expression: Expression): AggregateFunction
```

Creates an aggregation that counts the number of stage inputs with valid evaluations of the provided `expression`.

| Parameters |
|---|---|
| `expression: Expression` | The expression to count. |

| Returns |
|---|---|
| `AggregateFunction` | A new `AggregateFunction` representing the 'count' aggregation. |

### count

```
fun count(fieldName: String): AggregateFunction
```

Creates an aggregation that counts the number of stage inputs where the input field exists.

| Parameters |
|---|---|
| `fieldName: String` | The name of the field to count. |

| Returns |
|---|---|
| `AggregateFunction` | A new `AggregateFunction` representing the 'count' aggregation. |

### countAll

```
fun countAll(): AggregateFunction
```

Creates an aggregation that counts the total number of stage inputs.

| Returns |
|---|---|
| `AggregateFunction` | A new `AggregateFunction` representing the countAll aggregation. |

### countDistinct

```
fun countDistinct(expression: Expression): AggregateFunction
```

Creates an aggregation that counts the number of distinct values of an expression across multiple stage inputs.

| Parameters |
|---|---|
| `expression: Expression` | The expression to count the distinct values of. |

| Returns |
|---|---|
| `AggregateFunction` | A new `AggregateFunction` representing the count distinct aggregation. |

### countDistinct

```
fun countDistinct(fieldName: String): AggregateFunction
```

Creates an aggregation that counts the number of distinct values of a field across multiple stage inputs.

| Parameters |
|---|---|
| `fieldName: String` | The name of the field to count the distinct values of. |

| Returns |
|---|---|
| `AggregateFunction` | A new `AggregateFunction` representing the count distinct aggregation. |

### countIf

```
fun countIf(condition: BooleanExpression): AggregateFunction
```

Creates an aggregation that counts the number of stage inputs where the provided boolean expression evaluates to true.

| Parameters |
|---|---|
| `condition: BooleanExpression` | The boolean expression to evaluate on each input. |

| Returns |
|---|---|
| `AggregateFunction` | A new `AggregateFunction` representing the count aggregation. |

### first

```
fun first(expression: Expression): AggregateFunction
```

Creates an aggregation that finds the first value of an expression across multiple stage inputs.

| Parameters |
|---|---|
| `expression: Expression` | The expression to find the first value of. |

| Returns |
|---|---|
| `AggregateFunction` | A new `AggregateFunction` representing the first aggregation. |

### first

```
fun first(fieldName: String): AggregateFunction
```

Creates an aggregation that finds the first value of a field across multiple stage inputs.

| Parameters |
|---|---|
| `fieldName: String` | The name of the field to find the first value of. |

| Returns |
|---|---|
| `AggregateFunction` | A new `AggregateFunction` representing the first aggregation. |

### last

```
fun last(expression: Expression): AggregateFunction
```

Creates an aggregation that finds the last value of an expression across multiple stage inputs.

| Parameters |
|---|---|
| `expression: Expression` | The expression to find the last value of. |

| Returns |
|---|---|
| `AggregateFunction` | A new `AggregateFunction` representing the last aggregation. |

### last

```
fun last(fieldName: String): AggregateFunction
```

Creates an aggregation that finds the last value of a field across multiple stage inputs.

| Parameters |
|---|---|
| `fieldName: String` | The name of the field to find the last value of. |

| Returns |
|---|---|
| `AggregateFunction` | A new `AggregateFunction` representing the last aggregation. |

### maximum

```
fun maximum(expression: Expression): AggregateFunction
```

Creates an aggregation that finds the maximum value of an expression across multiple stage inputs.

| Parameters |
|---|---|
| `expression: Expression` | The expression to find the maximum value of. |

| Returns |
|---|---|
| `AggregateFunction` | A new `AggregateFunction` representing the maximum aggregation. |

### maximum

```
fun maximum(fieldName: String): AggregateFunction
```

Creates an aggregation that finds the maximum value of a field across multiple stage inputs.

| Parameters |
|---|---|
| `fieldName: String` | The name of the field to find the maximum value of. |

| Returns |
|---|---|
| `AggregateFunction` | A new `AggregateFunction` representing the maximum aggregation. |

### minimum

```
fun minimum(expression: Expression): AggregateFunction
```

Creates an aggregation that finds the minimum value of an expression across multiple stage inputs.

| Parameters |
|---|---|
| `expression: Expression` | The expression to find the minimum value of. |

| Returns |
|---|---|
| `AggregateFunction` | A new `AggregateFunction` representing the minimum aggregation. |

### minimum

```
fun minimum(fieldName: String): AggregateFunction
```

Creates an aggregation that finds the minimum value of a field across multiple stage inputs.

| Parameters |
|---|---|
| `fieldName: String` | The name of the field to find the minimum value of. |

| Returns |
|---|---|
| `AggregateFunction` | A new `AggregateFunction` representing the minimum aggregation. |

### rawAggregate

```
fun rawAggregate(name: String, vararg expr: Expression): AggregateFunction
```

Creates a raw aggregation function.

This method provides a way to call aggregation functions that are supported by the Firestore backend but that are not available as specific factory methods in this class.

| Parameters |
|---|---|
| `name: String` | The name of the aggregation function. |
| `vararg expr: Expression` | The expressions to pass as arguments to the function. |

| Returns |
|---|---|
| `AggregateFunction` | A new `AggregateFunction` for the specified function. |

### sum

```
fun sum(expression: Expression): AggregateFunction
```

Creates an aggregation that calculates the sum of values from an expression across multiple stage inputs.

| Parameters |
|---|---|
| `expression: Expression` | The expression to sum up. |

| Returns |
|---|---|
| `AggregateFunction` | A new `AggregateFunction` representing the sum aggregation. |

### sum

```
fun sum(fieldName: String): AggregateFunction
```

Creates an aggregation that calculates the sum of a field's values across multiple stage inputs.

| Parameters |
|---|---|
| `fieldName: String` | The name of the field containing numeric values to sum up. |

| Returns |
|---|---|
| `AggregateFunction` | A new `AggregateFunction` representing the sum aggregation. |

## Public functions

### alias

```
fun alias(alias: String): AliasedAggregate
```

Assigns an alias to this aggregate.

| Parameters |
|---|---|
| `alias: String` | The alias to assign to this aggregate. |

| Returns |
|---|---|
| `AliasedAggregate` | A new `AliasedAggregate` that wraps this aggregate and associates it with the provided alias. |

### over

```
@Beta
fun over(window: WindowSpec): WindowFunction
```

Applies a window frame to this aggregate, turning it into a window function.

Window Functions SDK features are in preview until the backend feature launches. Look for launch announcement at: https://docs.cloud.google.com/firestore/docs/release-notes

Use this to give a single accumulator its own framing, independent of the frame declared on the enclosing `addWindowFields` stage. Only `documents` or `range` window frames (created via `WindowSpec.documents` or `WindowSpec.range`) are supported on individual accumulators; other window parameters such as `WindowSpec.partition` or `WindowSpec.sort` are not supported on accumulator-level `over()` and must be specified on the enclosing `addWindowFields` stage.

| Parameters |
|---|---|
| `window: WindowSpec` | The window frame specification to evaluate this aggregate over. |

| Returns |
|---|---|
| `WindowFunction` | A new `WindowFunction` wrapping this aggregate. |