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

A class that represents an aggregate function.

**Signature:**

```typescript
export declare class AggregateFunction 
```

## Constructors

| Constructor | Modifiers | Description |
|---|---|---|
| [(constructor)(name, params)](https://firebase.google.com/docs/reference/js/firestore_pipelines.aggregatefunction.md.txt#aggregatefunctionconstructor) |   | Constructs a new instance of the `AggregateFunction` class |

## Properties

| Property | Modifiers | Type | Description |
|---|---|---|---|
| [exprType](https://firebase.google.com/docs/reference/js/firestore_pipelines.aggregatefunction.md.txt#aggregatefunctionexprtype) |   | [ExpressionType](https://firebase.google.com/docs/reference/js/firestore_pipelines.md.txt#expressiontype) |   |

## Methods

| Method | Modifiers | Description |
|---|---|---|
| [as(name)](https://firebase.google.com/docs/reference/js/firestore_pipelines.aggregatefunction.md.txt#aggregatefunctionas) |   | Assigns an alias to this AggregateFunction. The alias specifies the name that the aggregated value will have in the output document. |
| [over(frame)](https://firebase.google.com/docs/reference/js/firestore_pipelines.aggregatefunction.md.txt#aggregatefunctionover) |   | ***(Public Preview)*** Evaluates this aggregate function over a specific window frame, rather than over the window frame defined by the enclosing [Pipeline.addWindowFields()](https://firebase.google.com/docs/reference/js/firestore_lite_pipelines.pipeline.md.txt#pipelineaddwindowfields) stage.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-notesThe backend only accepts a frame (`documents` or `range`) here, and rejects `partition` and `sort`, which must be specified on the enclosing `addWindowFields()` stage.- In a `documents` frame, bounds are document offsets relative to the current document's position (`'current'` refers strictly to the current document's position; tied documents are not included). For `preceding`, a positive integer (e.g. `2`) includes up to that many documents before the current document, and a negative integer (e.g. `-1`) indicates a boundary following the current document, enabling frames that start after the current document. For `following`, a positive integer (e.g. `2`) includes up to that many documents after the current document, and a negative integer (e.g. `-1`) indicates a boundary preceding the current document, enabling frames that end before the current document (e.g. excluding the current document). - In a `range` frame, bounds are value or time offsets relative to the current document's sort value(s) (`'current'` is peer-inclusive and includes all documents tied with the current document's sort value(s), equivalent to an offset of `0`). For `preceding`, a positive offset subtracts from the current document's sort value (looking into the past), and a negative offset adds to the current document's sort value (shifting the lower boundary past the current document). For `following`, a positive offset adds to the current document's sort value (looking into the future), and a negative offset subtracts from the current document's sort value (shifting the upper boundary before the current document). Range frames with numeric or time-unit offsets require a single numeric or timestamp `sort` ordering on the enclosing stage, whereas range frames bounded only by `'current'` and `'unbounded'` support multiple `sort` orderings and non-numeric sort values. |

## AggregateFunction.(constructor)

Constructs a new instance of the `AggregateFunction` class

**Signature:**

```typescript
constructor(name: string, params: Expression[]);
```

#### Parameters

| Parameter | Type | Description |
|---|---|---|
| name | string |   |
| params | [Expression](https://firebase.google.com/docs/reference/js/firestore_pipelines.expression.md.txt#expression_class)\[\] |   |

## AggregateFunction.exprType

**Signature:**

```typescript
exprType: ExpressionType;
```

## AggregateFunction.as()

Assigns an alias to this AggregateFunction. The alias specifies the name that the aggregated value will have in the output document.

**Signature:**

```typescript
as(name: string): AliasedAggregate;
```

#### Parameters

| Parameter | Type | Description |
|---|---|---|
| name | string | The alias to assign to this AggregateFunction. |

**Returns:**

[AliasedAggregate](https://firebase.google.com/docs/reference/js/firestore_pipelines.aliasedaggregate.md.txt#aliasedaggregate_class)

A new [AliasedAggregate](https://firebase.google.com/docs/reference/js/firestore_lite_pipelines.aliasedaggregate.md.txt#aliasedaggregate_class) that wraps this AggregateFunction and associates it with the provided alias.

### Example

```typescript
// Calculate the average price of all items and assign it the alias "averagePrice".
firestore.pipeline().collection("items")
  .aggregate(field("price").average().as("averagePrice"));

```

## AggregateFunction.over()

> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.

Evaluates this aggregate function over a specific window frame, rather than over the window frame defined by the enclosing [Pipeline.addWindowFields()](https://firebase.google.com/docs/reference/js/firestore_lite_pipelines.pipeline.md.txt#pipelineaddwindowfields) stage.

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

The backend only accepts a frame (`documents` or `range`) here, and rejects `partition` and `sort`, which must be specified on the enclosing `addWindowFields()` stage.

- In a `documents` frame, bounds are document offsets relative to the current document's position (`'current'` refers strictly to the current document's position; tied documents are not included). For `preceding`, a positive integer (e.g. `2`) includes up to that many documents before the current document, and a negative integer (e.g. `-1`) indicates a boundary following the current document, enabling frames that start after the current document. For `following`, a positive integer (e.g. `2`) includes up to that many documents after the current document, and a negative integer (e.g. `-1`) indicates a boundary preceding the current document, enabling frames that end before the current document (e.g. excluding the current document). - In a `range` frame, bounds are value or time offsets relative to the current document's sort value(s) (`'current'` is peer-inclusive and includes all documents tied with the current document's sort value(s), equivalent to an offset of `0`). For `preceding`, a positive offset subtracts from the current document's sort value (looking into the past), and a negative offset adds to the current document's sort value (shifting the lower boundary past the current document). For `following`, a positive offset adds to the current document's sort value (looking into the future), and a negative offset subtracts from the current document's sort value (shifting the upper boundary before the current document). Range frames with numeric or time-unit offsets require a single numeric or timestamp `sort` ordering on the enclosing stage, whereas range frames bounded only by `'current'` and `'unbounded'` support multiple `sort` orderings and non-numeric sort values.

**Signature:**

```typescript
over(
    frame: OneOf<{
      documents: DocumentWindowFrame;
      range: RangeWindowFrame;
    }>
  ): WindowFunction;
```

#### Parameters

| Parameter | Type | Description |
|---|---|---|
| frame | [OneOf](https://firebase.google.com/docs/reference/js/firestore_pipelines.md.txt#oneof)\<{ documents: [DocumentWindowFrame](https://firebase.google.com/docs/reference/js/firestore_pipelines.documentwindowframe.md.txt#documentwindowframe_interface); range: [RangeWindowFrame](https://firebase.google.com/docs/reference/js/firestore_pipelines.rangewindowframe.md.txt#rangewindowframe_interface); }\> | The `documents` or `range` window frame to evaluate this aggregate over. |

**Returns:**

[WindowFunction](https://firebase.google.com/docs/reference/js/firestore_pipelines.windowfunction.md.txt#windowfunction_class)

A new [WindowFunction](https://firebase.google.com/docs/reference/js/firestore_lite_pipelines.windowfunction.md.txt#windowfunction_class).

### Example

```typescript
firestore.pipeline().collection("sales")
  .addWindowFields(
    { sort: ascending('date') },
    // Cumulative sum up to the current date (including tied peers).
    sum('amount')
      .over({ range: { preceding: 'unbounded', following: 'current' } })
      .as('runningTotal'),
    // 3-document moving window centered on the current document.
    average('amount')
      .over({ documents: { preceding: 1, following: 1 } })
      .as('movingAverage')
  );

```