The Pipeline class provides a flexible and expressive framework for building complex data transformation and query pipelines for Firestore.
A pipeline takes data sources, such as Firestore collections or collection groups, and applies a series of stages that are chained together. Each stage takes the output from the previous stage (or the data source) and produces an output for the next stage (or as the final output of the pipeline).
Expressions can be used within each stage to filter and transform data through the stage.
NOTE: The chained stages do not prescribe exactly how Firestore will execute the pipeline. Instead, Firestore only guarantees that the result is the same as if the chained stages were executed in order.
Usage Examples:
Signature:
export declare class Pipeline
Methods
| Method | Modifiers | Description |
|---|---|---|
| addFields(field, additionalFields) | Adds new fields to outputs from previous stages.This stage allows you to compute values on-the-fly based on existing data from previous stages or constants. You can use this to create new fields or overwrite existing ones (if there is name overlaps).The added fields are defined using Selectables, which can be:- Field: References an existing document field. - Expression: Either a literal value (see constant()) or a computed value with an assigned alias using Expression.as().Example: | |
| addFields(options) | Adds new fields to outputs from previous stages.This stage allows you to compute values on-the-fly based on existing data from previous stages or constants. You can use this to create new fields or overwrite existing ones (if there is name overlaps).The added fields are defined using Selectables, which can be:- Field: References an existing document field. - Expression: Either a literal value (see constant()) or a computed value with an assigned alias using Expression.as().Example: | |
| aggregate(accumulator, additionalAccumulators) | Performs aggregation operations on the documents from previous stages. This stage allows you to calculate aggregate values over a set of documents. You define the aggregations to perform using AliasedAggregate expressions which are typically results of calling Expression.as() on AggregateFunction instances. Example: |
|
| aggregate(options) | Performs optionally grouped aggregation operations on the documents from previous stages. This stage allows you to calculate aggregate values over a set of documents, optionally grouped by one or more fields or functions. You can specify:
Example: |
|
| define(aliasedExpression, additionalExpressions) | Defines one or more variables in the pipeline's scope. define is used to bind a value to a variable for internal reuse within the pipeline body (accessed via the variable() function).This stage is useful for declaring reusable values or intermediate calculations that can be referenced multiple times in later parts of the pipeline, improving readability and maintainability.Each variable is defined using an AliasedExpression, which pairs an expression with a name (alias). The expression can be a simple constant, a field reference, or a complex computation. |
|
| define(options) | Defines one or more variables in the pipeline's scope. define is used to bind a value to a variable for internal reuse within the pipeline body (accessed via the variable() function).This stage is useful for declaring reusable values or intermediate calculations that can be referenced multiple times in later parts of the pipeline, improving readability and maintainability.Each variable is defined using an AliasedExpression, which pairs an expression with a name (alias). The expression can be a simple constant, a field reference, or a complex computation. |
|
| distinct(group, additionalGroups) | Returns a set of distinct values from the inputs to this stage.This stage runs through the results from previous stages to include only results with unique combinations of Expression values (Field, AliasedExpression, etc).The parameters to this stage are defined using Selectable expressions or strings:- string: Name of an existing field - Field: References an existing document field. - AliasedExpression: Represents the result of a function with an assigned alias name using Expression.as().Example: |
|
| distinct(options) | Returns a set of distinct values from the inputs to this stage.This stage runs through the results from previous stages to include only results with unique combinations of Expression values (Field, AliasedExpression, etc).The parameters to this stage are defined using Selectable expressions or strings:- string: Name of an existing field - Field: References an existing document field. - AliasedExpression: Represents the result of a function with an assigned alias name using Expression.as().Example: |
|
| findNearest(options) | Performs a vector proximity search on the documents from the previous stage, returning the K-nearest documents based on the specified query vectorValue and distanceMeasure. The returned documents will be sorted in order from nearest to furthest from the query vectorValue.Example: |
// Find the 10 most similar books based on the book description.
const bookDescription = "Lorem ipsum...";
const queryVector: number[] = ...; // compute embedding of `bookDescription`
firestore.pipeline().collection("books")
.findNearest({
field: 'embedding',
vectorValue: queryVector,
distanceMeasure: 'euclidean',
limit: 10, // optional
distanceField: 'computedDistance' // optional
});
|
| limit(limit) | | Limits the maximum number of documents returned by previous stages to limit.
This stage is particularly useful when you want to retrieve a controlled subset of data from a potentially large result set. It's often used for:
- **Pagination:** In combination with to retrieve specific pages of results.
- **Limiting Data Retrieval:** To prevent excessive data transfer and improve performance, especially when dealing with large collections.
Example: |
| limit(options) | | Limits the maximum number of documents returned by previous stages to limit.
This stage is particularly useful when you want to retrieve a controlled subset of data from a potentially large result set. It's often used for:
- **Pagination:** In combination with to retrieve specific pages of results.
- **Limiting Data Retrieval:** To prevent excessive data transfer and improve performance, especially when dealing with large collections.
Example: |
| offset(offset) | | Skips the first offset number of documents from the results of previous stages.
This stage is useful for implementing pagination in your pipelines, allowing you to retrieve results in chunks. It is typically used in conjunction with to control the size of each page.
Example: |
| offset(options) | | Skips the first offset number of documents from the results of previous stages.
This stage is useful for implementing pagination in your pipelines, allowing you to retrieve results in chunks. It is typically used in conjunction with to control the size of each page.
Example: | | rawStage(name, params, options) | | Adds a raw stage to the pipeline.
This method provides a flexible way to extend the pipeline's functionality by adding custom stages. Each raw stage is defined by a unique name and a set of params that control its behavior.
Example (Assuming there is no 'where' stage available in SDK): | | removeFields(fieldValue, additionalFields) | | Remove fields from outputs of previous stages.Example: | | removeFields(options) | | Remove fields from outputs of previous stages.Example: | | replaceWith(fieldName) | | Fully overwrites all fields in a document with those coming from a nested map.
This stage allows you to emit a map value as a document. Each key of the map becomes a field on the document that contains the corresponding value.
Example: | | replaceWith(expr) | | Fully overwrites all fields in a document with those coming from a map.
This stage allows you to emit a map value as a document. Each key of the map becomes a field on the document that contains the corresponding value.
Example: | | replaceWith(options) | | Fully overwrites all fields in a document with those coming from a map.
This stage allows you to emit a map value as a document. Each key of the map becomes a field on the document that contains the corresponding value.
Example: | | sample(documents) | | Performs a pseudo-random sampling of the documents from the previous stage.
This stage will filter documents pseudo-randomly. The parameter specifies how number of documents to be returned.
Examples: | | sample(options) | | Performs a pseudo-random sampling of the documents from the previous stage.
This stage will filter documents pseudo-randomly. The 'options' parameter specifies how sampling will be performed. See SampleStageOptions for more information. | | search(options) | | (Public Preview) Add a search stage to the Pipeline. The search stage supports full-text search and geo search expressions. | | select(selection, additionalSelections) | | Selects or creates a set of fields from the outputs of previous stages.
The selected fields are defined using Selectable expressions, which can be:
string: Name of an existing field- Field: References an existing field.
- AliasedExpression: Represents the result of a function with an assigned alias name using Expression.as()
If no selections are provided, the output of this stage is empty. Use Pipeline.addFields() instead if only additions are desired.
Example: | | select(options) | | Selects or creates a set of fields from the outputs of previous stages.
The selected fields are defined using Selectable expressions, which can be:
string: Name of an existing field- Field: References an existing field.
- AliasedExpression: Represents the result of a function with an assigned alias name using Expression.as()
If no selections are provided, the output of this stage is empty. Use Pipeline.addFields() instead if only additions are desired.
Example: | | sort(ordering, additionalOrderings) | | Sorts the documents from previous stages based on one or more Ordering criteria.
This stage allows you to order the results of your pipeline. You can specify multiple Ordering instances to sort by multiple fields in ascending or descending order. If documents have the same value for a field used for sorting, the next specified ordering will be used. If all orderings result in equal comparison, the documents are considered equal and the order is unspecified.
Example: | | sort(options) | | Sorts the documents from previous stages based on one or more Ordering criteria.
This stage allows you to order the results of your pipeline. You can specify multiple Ordering instances to sort by multiple fields in ascending or descending order. If documents have the same value for a field used for sorting, the next specified ordering will be used. If all orderings result in equal comparison, the documents are considered equal and the order is unspecified.
Example: | | toArrayExpression() | | Converts this Pipeline into an expression that evaluates to an array of results.
Result Unwrapping:
- If the items have a single field, their values are unwrapped and returned directly in the array.
- If the items have multiple fields, they are returned as objects in the array
Runtime Validation: The runtime validates that the result set contains zero or one item. If zero items, it evaluates to null.
Result Unwrapping:
- If the item has a single field, its value is unwrapped and returned directly.
- f the item has multiple fields, they are returned as an object.
This stage will pass through documents from previous stage, and also pass through documents from previous stage of the other Pipeline given in parameter. The order of documents emitted from this stage is undefined.
Example: | | union(options) | | Performs union of all documents from two pipelines, including duplicates.
This stage will pass through documents from previous stage, and also pass through documents from previous stage of the other Pipeline given in parameter. The order of documents emitted from this stage is undefined.
Example: |
| unnest(selectable, indexField) | | Produces a document for each element in an input array.For each previous stage document, this stage will emit zero or more augmented documents. The input array specified by the selectable parameter, will emit an augmented document for each input array element. The input array element will augment the previous stage document by setting the alias field with the array element value.When selectable evaluates to a non-array value (ex: number, null, absent), then the stage becomes a no-op for the current input document, returning it as is with the alias field absent.No documents are emitted when selectable evaluates to an empty array.Example: |
| unnest(options) | | Produces a document for each element in an input array.For each previous stage document, this stage will emit zero or more augmented documents. The input array specified by the selectable parameter, will emit an augmented document for each input array element. The input array element will augment the previous stage document by setting the alias field with the array element value.When selectable evaluates to a non-array value (ex: number, null, absent), then the stage becomes a no-op for the current input document, returning it as is with the alias field absent.No documents are emitted when selectable evaluates to an empty array.Example: |
| where(condition) | | Filters the documents from previous stages to only include those matching the specified BooleanExpression.
This stage allows you to apply conditions to the data, similar to a "WHERE" clause in SQL. You can filter documents based on their field values, using implementations of BooleanExpression, typically including but not limited to:
- field comparators: Expression.equal(), Expression.lessThan(), Expression.greaterThan(), etc.
- logical operators: , , , etc.
- advanced functions: Expression.regexMatch(), Expression.arrayContains(), etc.
Example: | | where(options) | | Filters the documents from previous stages to only include those matching the specified BooleanExpression.
This stage allows you to apply conditions to the data, similar to a "WHERE" clause in SQL. You can filter documents based on their field values, using implementations of BooleanExpression, typically including but not limited to:
- field comparators: , (less than), Expression.greaterThan(), etc.
- logical operators: , , , etc.
- advanced functions: Expression.regexMatch(), Expression.arrayContains(), etc.
Example: |
Pipeline.addFields()
Adds new fields to outputs from previous stages.
This stage allows you to compute values on-the-fly based on existing data from previous stages or constants. You can use this to create new fields or overwrite existing ones (if there is name overlaps).
The added fields are defined using Selectables, which can be:
- Field: References an existing document field. - Expression: Either a literal value (see constant()) or a computed value with an assigned alias using Expression.as().
Example:
Signature:
addFields(field: Selectable, ...additionalFields: Selectable[]): Pipeline;
Parameters
| Parameter | Type | Description |
|---|---|---|
| field | Selectable | The first field to add to the documents, specified as a Selectable. |
| additionalFields | Selectable[] | Optional additional fields to add to the documents, specified as Selectables. |
Returns:
A new Pipeline object with this stage appended to the stage list.
Example
firestore.pipeline().collection("books")
.addFields(
field("rating").as("bookRating"), // Rename 'rating' to 'bookRating'
add(field("quantity"), 5).as("totalCost") // Calculate 'totalCost'
);
Pipeline.addFields()
Adds new fields to outputs from previous stages.
This stage allows you to compute values on-the-fly based on existing data from previous stages or constants. You can use this to create new fields or overwrite existing ones (if there is name overlaps).
The added fields are defined using Selectables, which can be:
- Field: References an existing document field. - Expression: Either a literal value (see constant()) or a computed value with an assigned alias using Expression.as().
Example:
Signature:
addFields(options: AddFieldsStageOptions): Pipeline;
Parameters
| Parameter | Type | Description |
|---|---|---|
| options | AddFieldsStageOptions | An object that specifies required and optional parameters for the stage. |
Returns:
A new Pipeline object with this stage appended to the stage list.
Example
firestore.pipeline().collection("books")
.addFields(
field("rating").as("bookRating"), // Rename 'rating' to 'bookRating'
add(field("quantity"), 5).as("totalCost") // Calculate 'totalCost'
);
Pipeline.aggregate()
Performs aggregation operations on the documents from previous stages.
This stage allows you to calculate aggregate values over a set of documents. You define the aggregations to perform using [AliasedAggregate](./firestore_pipelines.aliasedaggregate.md#aliasedaggregate_class) expressions which are typically results of calling [Expression.as()](./firestore_pipelines.expression.md#expressionas) on [AggregateFunction](./firestore_pipelines.aggregatefunction.md#aggregatefunction_class) instances.
Example: Signature: ```typescript aggregate(accumulator: AliasedAggregate, ...additionalAccumulators: AliasedAggregate[]): Pipeline; ``` #### Parameters | Parameter | Type | Description | | --- | --- | --- | | accumulator | [AliasedAggregate](./firestore_lite_pipelines.aliasedaggregate.md#aliasedaggregate_class) | The first [AliasedAggregate](./firestore_pipelines.aliasedaggregate.md#aliasedaggregate_class), wrapping an [AggregateFunction](./firestore_pipelines.aggregatefunction.md#aggregatefunction_class) and providing a name for the accumulated results. | | additionalAccumulators | [AliasedAggregate](./firestore_lite_pipelines.aliasedaggregate.md#aliasedaggregate_class)\[\] | Optional additional [AliasedAggregate](./firestore_pipelines.aliasedaggregate.md#aliasedaggregate_class), each wrapping an [AggregateFunction](./firestore_pipelines.aggregatefunction.md#aggregatefunction_class) and providing a name for the accumulated results. | Returns: [Pipeline](./firestore_lite_pipelines.pipeline.md#pipeline_class) A new Pipeline object with this stage appended to the stage list. ### Example ```typescript // Calculate the average rating and the total number of books firestore.pipeline().collection("books") .aggregate( field("rating").average().as("averageRating"), countAll().as("totalBooks") ); ``` ## Pipeline.aggregate() Performs optionally grouped aggregation operations on the documents from previous stages.
This stage allows you to calculate aggregate values over a set of documents, optionally grouped by one or more fields or functions. You can specify:
- \*\*Grouping Fields or Functions:\*\* One or more fields or functions to group the documents by. For each distinct combination of values in these fields, a separate group is created. If no grouping fields are provided, a single group containing all documents is used. Not specifying groups is the same as putting the entire inputs into one group.
- \*\*Accumulators:\*\* One or more accumulation operations to perform within each group. These are defined using [AliasedAggregate](./firestore_pipelines.aliasedaggregate.md#aliasedaggregate_class) expressions, which are typically created by calling [Expression.as()](./firestore_pipelines.expression.md#expressionas) on [AggregateFunction](./firestore_pipelines.aggregatefunction.md#aggregatefunction_class) instances. Each aggregation calculates a value (e.g., sum, average, count) based on the documents within its group.
Example:
Signature:
```typescript
aggregate(options: AggregateStageOptions): Pipeline;
```
#### Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| options | [AggregateStageOptions](./firestore_lite_pipelines.md#aggregatestageoptions) | An object that specifies required and optional parameters for the stage. |
Returns:
[Pipeline](./firestore_lite_pipelines.pipeline.md#pipeline_class)
A new [Pipeline](./firestore_pipelines.pipeline.md#pipeline_class) object with this stage appended to the stage list.
### Example
```typescript
// Calculate the average rating for each genre.
firestore.pipeline().collection("books")
.aggregate({
accumulators: [average(field("rating")).as("avg_rating")],
groups: ["genre"]
});
```
## Pipeline.define()
Defines one or more variables in the pipeline's scope. `define` is used to bind a value to a variable for internal reuse within the pipeline body (accessed via the `variable()` function).
This stage is useful for declaring reusable values or intermediate calculations that can be referenced multiple times in later parts of the pipeline, improving readability and maintainability.
Each variable is defined using an [AliasedExpression](./firestore_pipelines.aliasedexpression.md#aliasedexpression_class), which pairs an expression with a name (alias). The expression can be a simple constant, a field reference, or a complex computation.
Signature:
```typescript
define(aliasedExpression: AliasedExpression, ...additionalExpressions: AliasedExpression[]): Pipeline;
```
#### Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| aliasedExpression | [AliasedExpression](./firestore_lite_pipelines.aliasedexpression.md#aliasedexpression_class) | The first expression to bind to a variable. |
| additionalExpressions | [AliasedExpression](./firestore_lite_pipelines.aliasedexpression.md#aliasedexpression_class)\[\] | Optional additional expression to bind to a variable. |
Returns:
[Pipeline](./firestore_lite_pipelines.pipeline.md#pipeline_class)
A new Pipeline object with this stage appended to the stage list.
### Example
```typescript
db.pipeline().collection("products")
.define(
field("price").multiply(0.9).as("discountedPrice"),
field("stock").add(10).as("newStock")
)
.where(variable("discountedPrice").lessThan(100))
.select(field("name"), variable("newStock"));
```
## Pipeline.define()
Defines one or more variables in the pipeline's scope. `define` is used to bind a value to a variable for internal reuse within the pipeline body (accessed via the `variable()` function).
This stage is useful for declaring reusable values or intermediate calculations that can be referenced multiple times in later parts of the pipeline, improving readability and maintainability.
Each variable is defined using an [AliasedExpression](./firestore_pipelines.aliasedexpression.md#aliasedexpression_class), which pairs an expression with a name (alias). The expression can be a simple constant, a field reference, or a complex computation.
Signature:
```typescript
define(options: DefineStageOptions): Pipeline;
```
#### Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| options | [DefineStageOptions](./firestore_lite_pipelines.md#definestageoptions) | An object that specifies required and optional parameters for the stage. |
Returns:
[Pipeline](./firestore_lite_pipelines.pipeline.md#pipeline_class)
A new Pipeline object with this stage appended to the stage list.
### Example
```typescript
db.pipeline().collection("products")
.define(
field("price").multiply(0.9).as("discountedPrice"),
field("stock").add(10).as("newStock")
)
.where(variable("discountedPrice").lessThan(100))
.select(field("name"), variable("newStock"));
```
## Pipeline.distinct()
Returns a set of distinct values from the inputs to this stage.
This stage runs through the results from previous stages to include only results with unique combinations of [Expression](./firestore_pipelines.expression.md#expression_class) values ([Field](./firestore_pipelines.field.md#field_class), [AliasedExpression](./firestore_pipelines.aliasedexpression.md#aliasedexpression_class), etc).
The parameters to this stage are defined using [Selectable](./firestore_pipelines.selectable.md#selectable_interface) expressions or strings:
- `string`: Name of an existing field - [Field](./firestore_pipelines.field.md#field_class): References an existing document field. - [AliasedExpression](./firestore_pipelines.aliasedexpression.md#aliasedexpression_class): Represents the result of a function with an assigned alias name using [Expression.as()](./firestore_pipelines.expression.md#expressionas).
Example:
Signature:
```typescript
distinct(group: string | Selectable, ...additionalGroups: Array Example:
```typescript
// Find the 10 most similar books based on the book description.
const bookDescription = "Lorem ipsum...";
const queryVector: number[] = ...; // compute embedding of `bookDescription`
firestore.pipeline().collection("books")
.findNearest({
field: 'embedding',
vectorValue: queryVector,
distanceMeasure: 'euclidean',
limit: 10, // optional
distanceField: 'computedDistance' // optional
});
```
Signature:
```typescript
findNearest(options: FindNearestStageOptions): Pipeline;
```
#### Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| options | [FindNearestStageOptions](./firestore_lite_pipelines.md#findneareststageoptions) | An object that specifies required and optional parameters for the stage. |
Returns:
[Pipeline](./firestore_lite_pipelines.pipeline.md#pipeline_class)
A new [Pipeline](./firestore_pipelines.pipeline.md#pipeline_class) object with this stage appended to the stage list.
## Pipeline.limit()
Limits the maximum number of documents returned by previous stages to `limit`.
This stage is particularly useful when you want to retrieve a controlled subset of data from a potentially large result set. It's often used for:
Example:
Signature:
```typescript
limit(limit: number): Pipeline;
```
#### Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| limit | number | The maximum number of documents to return. |
Returns:
[Pipeline](./firestore_lite_pipelines.pipeline.md#pipeline_class)
A new Pipeline object with this stage appended to the stage list.
### Example
```typescript
// Limit the results to the top 10 highest-rated books
firestore.pipeline().collection('books')
.sort(field('rating').descending())
.limit(10);
```
## Pipeline.limit()
Limits the maximum number of documents returned by previous stages to `limit`.
This stage is particularly useful when you want to retrieve a controlled subset of data from a potentially large result set. It's often used for:
Example:
Signature:
```typescript
limit(options: LimitStageOptions): Pipeline;
```
#### Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| options | [LimitStageOptions](./firestore_lite_pipelines.md#limitstageoptions) | An object that specifies required and optional parameters for the stage. |
Returns:
[Pipeline](./firestore_lite_pipelines.pipeline.md#pipeline_class)
A new Pipeline object with this stage appended to the stage list.
### Example
```typescript
// Limit the results to the top 10 highest-rated books
firestore.pipeline().collection('books')
.sort(field('rating').descending())
.limit(10);
```
## Pipeline.offset()
Skips the first `offset` number of documents from the results of previous stages.
This stage is useful for implementing pagination in your pipelines, allowing you to retrieve results in chunks. It is typically used in conjunction with to control the size of each page.
Example:
Signature:
```typescript
offset(offset: number): Pipeline;
```
#### Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| offset | number | The number of documents to skip. |
Returns:
[Pipeline](./firestore_lite_pipelines.pipeline.md#pipeline_class)
A new Pipeline object with this stage appended to the stage list.
### Example
```typescript
// Retrieve the second page of 20 results
firestore.pipeline().collection('books')
.sort(field('published').descending())
.offset(20) // Skip the first 20 results
.limit(20); // Take the next 20 results
```
## Pipeline.offset()
Skips the first `offset` number of documents from the results of previous stages.
This stage is useful for implementing pagination in your pipelines, allowing you to retrieve results in chunks. It is typically used in conjunction with to control the size of each page.
Example:
Signature:
```typescript
offset(options: OffsetStageOptions): Pipeline;
```
#### Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| options | [OffsetStageOptions](./firestore_lite_pipelines.md#offsetstageoptions) | An object that specifies required and optional parameters for the stage. |
Returns:
[Pipeline](./firestore_lite_pipelines.pipeline.md#pipeline_class)
A new Pipeline object with this stage appended to the stage list.
### Example
```typescript
// Retrieve the second page of 20 results
firestore.pipeline().collection('books')
.sort(field('published').descending())
.offset(20) // Skip the first 20 results
.limit(20); // Take the next 20 results
```
## Pipeline.rawStage()
Adds a raw stage to the pipeline.
This method provides a flexible way to extend the pipeline's functionality by adding custom stages. Each raw stage is defined by a unique `name` and a set of `params` that control its behavior.
Example (Assuming there is no 'where' stage available in SDK):
Signature:
```typescript
rawStage(name: string, params: unknown[], options?: {
[key: string]: Expression | unknown;
}): Pipeline;
```
#### Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| name | string | The unique name of the raw stage to add. |
| params | unknown\[\] | A list of parameters to configure the raw stage's behavior. |
| options | { \[key: string\]: [Expression](./firestore_lite_pipelines.expression.md#expression_class) \| unknown; } | An object of key value pairs that specifies optional parameters for the stage. |
Returns:
[Pipeline](./firestore_lite_pipelines.pipeline.md#pipeline_class)
A new [Pipeline](./firestore_pipelines.pipeline.md#pipeline_class) object with this stage appended to the stage list.
### Example
```typescript
// Assume we don't have a built-in 'where' stage
firestore.pipeline().collection('books')
.rawStage('where', [field('published').lessThan(1900)]) // Custom 'where' stage
.select('title', 'author');
```
## Pipeline.removeFields()
Remove fields from outputs of previous stages.
Example:
Signature:
```typescript
removeFields(fieldValue: Field | string, ...additionalFields: Array This stage allows you to emit a map value as a document. Each key of the map becomes a field on the document that contains the corresponding value.
Example:
Signature:
```typescript
replaceWith(fieldName: string): Pipeline;
```
#### Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| fieldName | string | The [Field](./firestore_pipelines.field.md#field_class) field containing the nested map. |
Returns:
[Pipeline](./firestore_lite_pipelines.pipeline.md#pipeline_class)
A new [Pipeline](./firestore_pipelines.pipeline.md#pipeline_class) object with this stage appended to the stage list.
### Example
```typescript
// Input.
// {
// 'name': 'John Doe Jr.',
// 'parents': {
// 'father': 'John Doe Sr.',
// 'mother': 'Jane Doe'
// }
// }
// Emit parents as document.
firestore.pipeline().collection('people').replaceWith('parents');
// Output
// {
// 'father': 'John Doe Sr.',
// 'mother': 'Jane Doe'
// }
```
## Pipeline.replaceWith()
Fully overwrites all fields in a document with those coming from a map.
This stage allows you to emit a map value as a document. Each key of the map becomes a field on the document that contains the corresponding value.
Example:
Signature:
```typescript
replaceWith(expr: Expression): Pipeline;
```
#### Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| expr | [Expression](./firestore_lite_pipelines.expression.md#expression_class) | An [Expression](./firestore_pipelines.expression.md#expression_class) that when returned evaluates to a map. |
Returns:
[Pipeline](./firestore_lite_pipelines.pipeline.md#pipeline_class)
A new [Pipeline](./firestore_pipelines.pipeline.md#pipeline_class) object with this stage appended to the stage list.
### Example
```typescript
// Input.
// {
// 'name': 'John Doe Jr.',
// 'parents': {
// 'father': 'John Doe Sr.',
// 'mother': 'Jane Doe'
// }
// }
// Emit parents as document.
firestore.pipeline().collection('people').replaceWith(map({
foo: 'bar',
info: {
name: field('name')
}
}));
// Output
// {
// 'father': 'John Doe Sr.',
// 'mother': 'Jane Doe'
// }
```
## Pipeline.replaceWith()
Fully overwrites all fields in a document with those coming from a map.
This stage allows you to emit a map value as a document. Each key of the map becomes a field on the document that contains the corresponding value.
Example:
Signature:
```typescript
replaceWith(options: ReplaceWithStageOptions): Pipeline;
```
#### Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| options | [ReplaceWithStageOptions](./firestore_lite_pipelines.md#replacewithstageoptions) | An object that specifies required and optional parameters for the stage. |
Returns:
[Pipeline](./firestore_lite_pipelines.pipeline.md#pipeline_class)
A new [Pipeline](./firestore_pipelines.pipeline.md#pipeline_class) object with this stage appended to the stage list.
### Example
```typescript
// Input.
// {
// 'name': 'John Doe Jr.',
// 'parents': {
// 'father': 'John Doe Sr.',
// 'mother': 'Jane Doe'
// }
// }
// Emit parents as document.
firestore.pipeline().collection('people').replaceWith(map({
foo: 'bar',
info: {
name: field('name')
}
}));
// Output
// {
// 'father': 'John Doe Sr.',
// 'mother': 'Jane Doe'
// }
```
## Pipeline.sample()
Performs a pseudo-random sampling of the documents from the previous stage.
This stage will filter documents pseudo-randomly. The parameter specifies how number of documents to be returned.
Examples:
Signature:
```typescript
sample(documents: number): Pipeline;
```
#### Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| documents | number | The number of documents to sample. |
Returns:
[Pipeline](./firestore_lite_pipelines.pipeline.md#pipeline_class)
A new [Pipeline](./firestore_pipelines.pipeline.md#pipeline_class) object with this stage appended to the stage list.
### Example
```typescript
// Sample 25 books, if available.
firestore.pipeline().collection('books')
.sample(25);
```
## Pipeline.sample()
Performs a pseudo-random sampling of the documents from the previous stage.
This stage will filter documents pseudo-randomly. The 'options' parameter specifies how sampling will be performed. See [SampleStageOptions](./firestore_pipelines.md#samplestageoptions) for more information.
Signature:
```typescript
sample(options: SampleStageOptions): Pipeline;
```
#### Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| options | [SampleStageOptions](./firestore_lite_pipelines.md#samplestageoptions) | An object that specifies required and optional parameters for the stage. |
Returns:
[Pipeline](./firestore_lite_pipelines.pipeline.md#pipeline_class)
A new [Pipeline](./firestore_pipelines.pipeline.md#pipeline_class) object with this stage appended to the stage list.
### Example
```typescript
// Sample 10 books, if available.
firestore.pipeline().collection("books")
.sample({ documents: 10 });
// Sample 50% of books.
firestore.pipeline().collection("books")
.sample({ percentage: 0.5 });
```
## Pipeline.search()
> 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.
>
Add a search stage to the Pipeline. The search stage supports full-text search and geo search expressions.
A limited set of expressions are supported in the search stage.
Signature:
```typescript
search(options: SearchStageOptions): Pipeline;
```
#### Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| options | [SearchStageOptions](./firestore_lite_pipelines.md#searchstageoptions) | An object that specifies parameters for the stage. A new The selected fields are defined using [Selectable](./firestore_pipelines.selectable.md#selectable_interface) expressions, which can be:
If no selections are provided, the output of this stage is empty. Use [Pipeline.addFields()](./firestore_pipelines.pipeline.md#pipelineaddfields) instead if only additions are desired.
Example:
Signature:
```typescript
select(selection: Selectable | string, ...additionalSelections: Array The selected fields are defined using [Selectable](./firestore_pipelines.selectable.md#selectable_interface) expressions, which can be:
If no selections are provided, the output of this stage is empty. Use [Pipeline.addFields()](./firestore_pipelines.pipeline.md#pipelineaddfields) instead if only additions are desired.
Example:
Signature:
```typescript
select(options: SelectStageOptions): Pipeline;
```
#### Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| options | [SelectStageOptions](./firestore_lite_pipelines.md#selectstageoptions) | An object that specifies required and optional parameters for the stage. |
Returns:
[Pipeline](./firestore_lite_pipelines.pipeline.md#pipeline_class)
A new Pipeline object with this stage appended to the stage list.
### Example
```typescript
db.pipeline().collection("books")
.select(
"firstName",
field("lastName"),
field("address").toUpper().as("upperAddress"),
);
```
## Pipeline.sort()
Sorts the documents from previous stages based on one or more [Ordering](./firestore_pipelines.ordering.md#ordering_class) criteria.
This stage allows you to order the results of your pipeline. You can specify multiple [Ordering](./firestore_pipelines.ordering.md#ordering_class) instances to sort by multiple fields in ascending or descending order. If documents have the same value for a field used for sorting, the next specified ordering will be used. If all orderings result in equal comparison, the documents are considered equal and the order is unspecified.
Example:
Signature:
```typescript
sort(ordering: Ordering, ...additionalOrderings: Ordering[]): Pipeline;
```
#### Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| ordering | [Ordering](./firestore_lite_pipelines.ordering.md#ordering_class) | The first [Ordering](./firestore_pipelines.ordering.md#ordering_class) instance specifying the sorting criteria. |
| additionalOrderings | [Ordering](./firestore_lite_pipelines.ordering.md#ordering_class)\[\] | Optional additional [Ordering](./firestore_pipelines.ordering.md#ordering_class) instances specifying the additional sorting criteria. |
Returns:
[Pipeline](./firestore_lite_pipelines.pipeline.md#pipeline_class)
A new [Pipeline](./firestore_pipelines.pipeline.md#pipeline_class) object with this stage appended to the stage list.
### Example
```typescript
// Sort books by rating in descending order, and then by title in ascending order for books
// with the same rating
firestore.pipeline().collection("books")
.sort(
field("rating").descending(),
field("title").ascending()
);
```
## Pipeline.sort()
Sorts the documents from previous stages based on one or more [Ordering](./firestore_pipelines.ordering.md#ordering_class) criteria.
This stage allows you to order the results of your pipeline. You can specify multiple [Ordering](./firestore_pipelines.ordering.md#ordering_class) instances to sort by multiple fields in ascending or descending order. If documents have the same value for a field used for sorting, the next specified ordering will be used. If all orderings result in equal comparison, the documents are considered equal and the order is unspecified.
Example:
Signature:
```typescript
sort(options: SortStageOptions): Pipeline;
```
#### Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| options | [SortStageOptions](./firestore_lite_pipelines.md#sortstageoptions) | An object that specifies required and optional parameters for the stage. |
Returns:
[Pipeline](./firestore_lite_pipelines.pipeline.md#pipeline_class)
A new [Pipeline](./firestore_pipelines.pipeline.md#pipeline_class) object with this stage appended to the stage list.
### Example
```typescript
// Sort books by rating in descending order, and then by title in ascending order for books
// with the same rating
firestore.pipeline().collection("books")
.sort(
field("rating").descending(),
field("title").ascending()
);
```
## Pipeline.toArrayExpression()
Converts this Pipeline into an expression that evaluates to an array of results.
Result Unwrapping: Runtime Validation: The runtime validates that the result set contains zero or one item. If zero items, it evaluates to `null`. Result Unwrapping: Signature: Returns: An Output: Multiple Fields: Output: Performs union of all documents from two pipelines, including duplicates. This stage will pass through documents from previous stage, and also pass through documents from previous stage of the Example: Signature: Returns: A new Pipeline object with this stage appended to the stage list. Performs union of all documents from two pipelines, including duplicates. This stage will pass through documents from previous stage, and also pass through documents from previous stage of the Example: Signature: Returns: A new Pipeline object with this stage appended to the stage list. Produces a document for each element in an input array. For each previous stage document, this stage will emit zero or more augmented documents. The input array specified by the When No documents are emitted when Example: Signature: Returns: A new Pipeline object with this stage appended to the stage list. Produces a document for each element in an input array. For each previous stage document, this stage will emit zero or more augmented documents. The input array specified by the When No documents are emitted when Example: Signature: Returns: A new Pipeline object with this stage appended to the stage list. Filters the documents from previous stages to only include those matching the specified BooleanExpression. This stage allows you to apply conditions to the data, similar to a "WHERE" clause in SQL. You can filter documents based on their field values, using implementations of BooleanExpression, typically including but not limited to: Example: Signature: Returns: A new Pipeline object with this stage appended to the stage list. Filters the documents from previous stages to only include those matching the specified BooleanExpression. This stage allows you to apply conditions to the data, similar to a "WHERE" clause in SQL. You can filter documents based on their field values, using implementations of BooleanExpression, typically including but not limited to: Example: Signature: Returns: A new Pipeline object with this stage appended to the stage list.
Pipeline object with this stage appended to the stage list. |
Returns:
[Pipeline](./firestore_lite_pipelines.pipeline.md#pipeline_class)
### Example 1
```typescript
// Full-text search example
firestore.pipeline().collection("restaurants")
.search({
query: documentMatches("waffles OR pancakes"),
sort: [
score().descending(),
],
addFields: [
score().as("searchScore"),
]
})
```
### Example 2
```typescript
// Geo distance search example
const queryLocation = new GeoPoint(0, 0);
db.pipeline().collection('restaurants').search({
query: field('location').geoDistance(queryLocation).lessThanOrEqual(1000),
sort: [
score().descending(),
],
})
```
## Pipeline.select()
Selects or creates a set of fields from the outputs of previous stages.
string values representing field names. |
Returns:
[Pipeline](./firestore_lite_pipelines.pipeline.md#pipeline_class)
A new Pipeline object with this stage appended to the stage list.
### Example
```typescript
db.pipeline().collection("books")
.select(
"firstName",
field("lastName"),
field("address").toUpper().as("upperAddress"),
);
```
## Pipeline.select()
Selects or creates a set of fields from the outputs of previous stages.
Signature:
```typescript
toArrayExpression(): Expression;
```
Returns:
[Expression](./firestore_lite_pipelines.expression.md#expression_class)
An `Expression` representing the execution of this pipeline.
### Example
```typescript
// Get a list of reviewers for each book
db.pipeline().collection("books")
.define(field("id").as("book_id"))
.addFields(
db.pipeline().collection("reviews")
.where(field("book_id").equal(variable("book_id")))
.select(field("reviewer"))
.toArrayExpression()
.as("reviewers")
)
```
Output:
```json
[
{
"id": "1",
"title": "1984",
"reviewers": ["Alice", "Bob"]
}
]
```
Multiple Fields:
```typescript
// Get a list of reviews (reviewer and rating) for each book
db.pipeline().collection("books")
.define(field("id").as("book_id"))
.addFields(
db.pipeline().collection("reviews")
.where(field("book_id").equal(variable("book_id")))
.select(field("reviewer"), field("rating"))
.toArrayExpression()
.as("reviews"))
```
Output:
```json
[
{
"id": "1",
"title": "1984",
"reviews": [
{ "reviewer": "Alice", "rating": 5 },
{ "reviewer": "Bob", "rating": 4 }
]
}
]
```
## Pipeline.toScalarExpression()
Converts this Pipeline into an expression that evaluates to a single scalar result.
toScalarExpression(): Expression;
Expression representing the execution of this pipeline.Example
// Calculate average rating for a restaurant
db.pipeline().collection("restaurants").addFields(
db.pipeline().collection("reviews")
.where(field("restaurant_id").equal(variable("rid")))
.aggregate(average("rating").as("avg"))
// Unwraps the single "avg" field to a scalar double
.toScalarExpression().as("average_rating")
)
{
"name": "The Burger Joint",
"average_rating": 4.5
}
// Calculate average rating AND count for a restaurant
db.pipeline().collection("restaurants").addFields(
db.pipeline().collection("reviews")
.where(field("restaurant_id").equal(variable("rid")))
.aggregate(
average("rating").as("avg"),
count().as("count")
)
// Returns an object with "avg" and "count" fields
.toScalarExpression().as("stats")
)
{
"name": "The Burger Joint",
"stats": {
"avg": 4.5,
"count": 100
}
}
Pipeline.union()
other Pipeline given in parameter. The order of documents emitted from this stage is undefined.union(other: Pipeline): Pipeline;
Parameters
Parameter
Type
Description
other
Pipeline
The other Pipeline that is part of union.
Example
// Emit documents from books collection and magazines collection.
firestore.pipeline().collection('books')
.union(firestore.pipeline().collection('magazines'));
Pipeline.union()
other Pipeline given in parameter. The order of documents emitted from this stage is undefined.union(options: UnionStageOptions): Pipeline;
Parameters
Parameter
Type
Description
options
UnionStageOptions
An object that specifies required and optional parameters for the stage.
Example
// Emit documents from books collection and magazines collection.
firestore.pipeline().collection('books')
.union(firestore.pipeline().collection('magazines'));
Pipeline.unnest()
selectable parameter, will emit an augmented document for each input array element. The input array element will augment the previous stage document by setting the alias field with the array element value.selectable evaluates to a non-array value (ex: number, null, absent), then the stage becomes a no-op for the current input document, returning it as is with the alias field absent.selectable evaluates to an empty array.unnest(selectable: Selectable, indexField?: string): Pipeline;
Parameters
Parameter
Type
Description
selectable
Selectable
A selectable expression defining the field to unnest and the alias to use for each un-nested element in the output documents.
indexField
string
An optional string value specifying the field path to write the offset (starting at zero) into the array the un-nested element is from
Example
// Input:
// { "title": "The Hitchhiker's Guide to the Galaxy", "tags": [ "comedy", "space", "adventure" ], ... }
// Emit a book document for each tag of the book.
firestore.pipeline().collection("books")
.unnest(field("tags").as('tag'), 'tagIndex');
// Output:
// { "title": "The Hitchhiker's Guide to the Galaxy", "tag": "comedy", "tagIndex": 0, ... }
// { "title": "The Hitchhiker's Guide to the Galaxy", "tag": "space", "tagIndex": 1, ... }
// { "title": "The Hitchhiker's Guide to the Galaxy", "tag": "adventure", "tagIndex": 2, ... }
Pipeline.unnest()
selectable parameter, will emit an augmented document for each input array element. The input array element will augment the previous stage document by setting the alias field with the array element value.selectable evaluates to a non-array value (ex: number, null, absent), then the stage becomes a no-op for the current input document, returning it as is with the alias field absent.selectable evaluates to an empty array.unnest(options: UnnestStageOptions): Pipeline;
Parameters
Parameter
Type
Description
options
UnnestStageOptions
An object that specifies required and optional parameters for the stage.
Example
// Input:
// { "title": "The Hitchhiker's Guide to the Galaxy", "tags": [ "comedy", "space", "adventure" ], ... }
// Emit a book document for each tag of the book.
firestore.pipeline().collection("books")
.unnest(field("tags").as('tag'), 'tagIndex');
// Output:
// { "title": "The Hitchhiker's Guide to the Galaxy", "tag": "comedy", "tagIndex": 0, ... }
// { "title": "The Hitchhiker's Guide to the Galaxy", "tag": "space", "tagIndex": 1, ... }
// { "title": "The Hitchhiker's Guide to the Galaxy", "tag": "adventure", "tagIndex": 2, ... }
Pipeline.where()
where(condition: BooleanExpression): Pipeline;
Parameters
Parameter
Type
Description
condition
BooleanExpression
The BooleanExpression to apply.
Example
firestore.pipeline().collection("books")
.where(
and(
greaterThan(field("rating"), 4.0), // Filter for ratings greater than 4.0
field("genre").equal("Science Fiction") // Equivalent to equal("genre", "Science Fiction")
)
);
Pipeline.where()
where(options: WhereStageOptions): Pipeline;
Parameters
Parameter
Type
Description
options
WhereStageOptions
An object that specifies required and optional parameters for the stage.
Example
firestore.pipeline().collection("books")
.where(
and(
greaterThan(field("rating"), 4.0), // Filter for ratings greater than 4.0
field("genre").equal("Science Fiction") // Equivalent to equal("genre", "Science Fiction")
)
);
Example
const db: Firestore; // Assumes a valid firestore instance.
// Example 1: Select specific fields and rename 'rating' to 'bookRating'
const results1 = await execute(db.pipeline()
.collection("books")
.select("title", "author", field("rating").as("bookRating")));
// Example 2: Filter documents where 'genre' is "Science Fiction" and 'published' is after 1950
const results2 = await execute(db.pipeline()
.collection("books")
.where(and(field("genre").equal("Science Fiction"), field("published").greaterThan(1950))));
// Example 3: Calculate the average rating of books published after 1980
const results3 = await execute(db.pipeline()
.collection("books")
.where(field("published").greaterThan(1980))
.aggregate(average(field("rating")).as("averageRating")));