A class that represents an aggregate function.
Signature:
export declare class AggregateFunction
Constructors
| Constructor | Modifiers | Description |
|---|---|---|
| (constructor)(name, params) | Constructs a new instance of the AggregateFunction class |
Properties
| Property | Modifiers | Type | Description |
|---|---|---|---|
| exprType | ExpressionType |
Methods
| Method | Modifiers | Description |
|---|---|---|
| as(name) | Assigns an alias to this AggregateFunction. The alias specifies the name that the aggregated value will have in the output document. | |
| over(frame) | (Public Preview) Evaluates this aggregate function over a specific window frame, rather than over the window frame defined by the enclosing Pipeline.addWindowFields() 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:
constructor(name: string, params: Expression[]);
Parameters
| Parameter | Type | Description |
|---|---|---|
| name | string | |
| params | Expression[] |
AggregateFunction.exprType
Signature:
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:
as(name: string): AliasedAggregate;
Parameters
| Parameter | Type | Description |
|---|---|---|
| name | string | The alias to assign to this AggregateFunction. |
Returns:
A new AliasedAggregate that wraps this AggregateFunction and associates it with the provided alias.
Example
// 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() 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
documentsframe, 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). Forpreceding, 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. Forfollowing, 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 arangeframe, 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 of0). Forpreceding, 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). Forfollowing, 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 timestampsortordering on the enclosing stage, whereas range frames bounded only by'current'and'unbounded'support multiplesortorderings and non-numeric sort values.
Signature:
over(
frame: OneOf<{
documents: DocumentWindowFrame;
range: RangeWindowFrame;
}>
): WindowFunction;
Parameters
| Parameter | Type | Description |
|---|---|---|
| frame | OneOf<{ documents: DocumentWindowFrame; range: RangeWindowFrame; }> | The documents or range window frame to evaluate this aggregate over. |
Returns:
A new WindowFunction.
Example
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')
);