Pipeline

@Beta
public final class Pipeline


A Pipeline is composed of a sequence of stages. Each stage processes the output from the previous one, and the final stage's output is the result of the pipeline's execution.

Example usage:

{@code Pipeline pipeline = firestore.pipeline()
.collection("books") .where(Field("rating").isGreaterThan(4.5))
.sort(Field("rating").descending()) .limit(2); }

Note on Execution: The stages are conceptual. The Firestore backend may optimize execution (e.g., reordering or merging stages) as long as the final result remains the same.

Important Limitations:

  • Pipelines operate on a request/response basis only.
  • They do not utilize or update the local SDK cache.
  • They do not support realtime snapshot listeners.

Summary

Nested types

public final class Pipeline.ExecuteOptions extends AbstractOptions
public final class Pipeline.Snapshot implements Iterable

A Snapshot contains the results of a pipeline execution.

Public methods

final @NonNull Pipeline
addFields(@NonNull Selectable field, @NonNull Selectable additionalFields)

Adds new fields to outputs from previous stages.

final @NonNull Pipeline
aggregate(@NonNull AggregateStage aggregateStage)

Performs optionally grouped aggregation operations on the documents from previous stages.

final @NonNull Pipeline
aggregate(
    @NonNull AliasedAggregate accumulator,
    @NonNull AliasedAggregate additionalAccumulators
)

Performs aggregation operations on the documents from previous stages.

final @NonNull Pipeline
aggregate(
    @NonNull AggregateStage aggregateStage,
    @NonNull AggregateOptions options
)

Performs optionally grouped aggregation operations on the documents from previous stages.

final @NonNull Pipeline
distinct(@NonNull Selectable group, @NonNull Object additionalGroups)

Returns a set of distinct values from the inputs to this stage.

final @NonNull Pipeline
distinct(@NonNull String groupField, @NonNull Object additionalGroups)

Returns a set of distinct values from the inputs to this stage.

final @NonNull Task<@NonNull Pipeline.Snapshot>

Executes this pipeline and returns the results as a Task of Snapshot.

final @NonNull Task<@NonNull Pipeline.Snapshot>

Executes this pipeline and returns the results as a Task of Snapshot.

final @NonNull Pipeline
findNearest(
    @NonNull Field vectorField,
    @NonNull double[] vectorValue,
    @NonNull FindNearestStage.DistanceMeasure distanceMeasure
)

Performs a vector similarity search, ordering the result set by most similar to least similar, and returning the full set in the order of similarity.

final @NonNull Pipeline
findNearest(
    @NonNull String vectorField,
    @NonNull double[] vectorValue,
    @NonNull FindNearestStage.DistanceMeasure distanceMeasure
)

Performs a vector similarity search, ordering the result set by most similar to least similar, and returning the full set in the order of similarity.

final @NonNull Pipeline
findNearest(
    @NonNull String vectorField,
    @NonNull Expression vectorValue,
    @NonNull FindNearestStage.DistanceMeasure distanceMeasure,
    @NonNull FindNearestOptions options
)

Performs a vector similarity search, ordering the result set by most similar to least similar, and returning the first N documents (specified by FindNearestOptions.withLimit) in the result set.

final @NonNull Pipeline
limit(int limit)

Limits the maximum number of documents returned by previous stages to limit.

final @NonNull Pipeline
offset(int offset)

Skips the first offset number of documents from the results of previous stages.

final @NonNull Pipeline

Adds a raw stage to the pipeline by specifying the stage name as an argument.

final @NonNull Pipeline
removeFields(@NonNull Field field, @NonNull Field additionalFields)

Remove fields from outputs of previous stages.

final @NonNull Pipeline
removeFields(@NonNull String field, @NonNull String additionalFields)

Remove fields from outputs of previous stages.

final @NonNull Pipeline

Fully overwrites all fields in a document with those coming from a nested map.

final @NonNull Pipeline

Fully overwrites all fields in a document with those coming from a nested map.

final @NonNull Pipeline
sample(int documents)

Performs a pseudo-random sampling of the input documents.

final @NonNull Pipeline

Performs a pseudo-random sampling of the input documents.

final @NonNull Pipeline
select(@NonNull String fieldName, @NonNull Object additionalSelections)

Selects or creates a set of fields from the outputs of previous stages.

final @NonNull Pipeline
select(@NonNull Selectable selection, @NonNull Object additionalSelections)

Selects or creates a set of fields from the outputs of previous stages.

final @NonNull Pipeline
sort(@NonNull Ordering order, @NonNull Ordering additionalOrders)

Sorts the documents from previous stages based on one or more Ordering criteria.

final @NonNull Pipeline

Performs union of all documents from two pipelines, including duplicates.

final @NonNull Pipeline
unnest(@NonNull Selectable arrayWithAlias)

Takes a specified array from the input documents and outputs a document for each element with the element stored in a field with name specified by the alias.

final @NonNull Pipeline
unnest(@NonNull UnnestStage unnestStage)

Takes a specified array from the input documents and outputs a document for each element with the element stored in a field with name specified by the alias.

final @NonNull Pipeline
unnest(@NonNull String arrayField, @NonNull String alias)

Takes a specified array from the input documents and outputs a document for each element with the element stored in a field with name specified by the alias.

final @NonNull Pipeline
unnest(@NonNull Selectable arrayWithAlias, @NonNull UnnestOptions options)

Takes a specified array from the input documents and outputs a document for each element with the element stored in a field with name specified by the alias.

final @NonNull Pipeline

Filters the documents from previous stages to only include those matching the specified BooleanExpression.

Public methods

addFields

public final @NonNull Pipeline addFields(@NonNull Selectable field, @NonNull Selectable 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.

The added fields are defined using Selectables, which can be:

  • Field: References an existing document field.

  • AliasedExpression: Represents the result of a expression with an assigned alias name using Expr.alias

Example:

firestore.pipeline().collection("books")
.addFields(
field("rating").as("bookRating"), // Rename 'rating' to 'bookRating'
add(5, field("quantity")).as("totalCost") // Calculate 'totalCost'
);
Parameters
@NonNull Selectable field

The first field to add to the documents, specified as a Selectable.

@NonNull Selectable additionalFields

The fields to add to the documents, specified as Selectables.

Returns
@NonNull Pipeline

A new Pipeline object with this stage appended to the stage list.

aggregate

public final @NonNull Pipeline aggregate(@NonNull AggregateStage aggregateStage)

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 Expressions: 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.

  • AggregateFunctions: One or more accumulation operations to perform within each group. These are defined using AliasedAggregate expressions, which are typically created by calling AggregateFunction.alias on AggregateFunction instances. Each aggregation calculates a value (e.g., sum, average, count) based on the documents within its group.

Example:

// Calculate the average rating for each genre.
firestore.pipeline().collection("books")
.aggregate(
Aggregate
.withAccumulators(average("rating").as("avg_rating"))
.withGroups("genre"));
Parameters
@NonNull AggregateStage aggregateStage

An AggregateStage object that specifies the grouping fields (if any) and the aggregation operations to perform.

Returns
@NonNull Pipeline

A new Pipeline object with this stage appended to the stage list.

aggregate

public final @NonNull Pipeline aggregate(
    @NonNull AliasedAggregate accumulator,
    @NonNull AliasedAggregate 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 AggregateFunction.alias on AggregateFunction instances.

Example:

// Calculate the average rating and the total number of books
firestore.pipeline().collection("books")
.aggregate(
field("rating").average().as("averageRating"),
countAll().as("totalBooks")
);
Parameters
@NonNull AliasedAggregate accumulator

The first AliasedAggregate expression, wrapping an AggregateFunction with an alias for the accumulated results.

@NonNull AliasedAggregate additionalAccumulators

The AliasedAggregate expressions, each wrapping an AggregateFunction with an alias for the accumulated results.

Returns
@NonNull Pipeline

A new Pipeline object with this stage appended to the stage list.

aggregate

public final @NonNull Pipeline aggregate(
    @NonNull AggregateStage aggregateStage,
    @NonNull AggregateOptions 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:

  • Grouping Fields or Expressions: 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.

  • AggregateFunctions: One or more accumulation operations to perform within each group. These are defined using AliasedAggregate expressions, which are typically created by calling AggregateFunction.alias on AggregateFunction instances. Each aggregation calculates a value (e.g., sum, average, count) based on the documents within its group.

Parameters
@NonNull AggregateStage aggregateStage

An AggregateStage object that specifies the grouping fields (if any) and the aggregation operations to perform.

@NonNull AggregateOptions options

The AggregateOptions to use when performing the aggregation.

Returns
@NonNull Pipeline

A new Pipeline object with this stage appended to the stage list.

distinct

public final @NonNull Pipeline distinct(@NonNull Selectable group, @NonNull Object 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 Expr values Field, FunctionExpression, 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 Expr.alias

Example:

// Get a list of unique author names in uppercase and genre combinations.
firestore.pipeline().collection("books")
.distinct(toUppercase(field("author")).as("authorName"), field("genre"))
.select("authorName");
Parameters
@NonNull Selectable group

The Selectable expression to consider when determining distinct value combinations.

@NonNull Object additionalGroups

The Selectable expressions to consider when determining distinct value combinations or Strings representing field names.

Returns
@NonNull Pipeline

A new Pipeline object with this stage appended to the stage list.

distinct

public final @NonNull Pipeline distinct(@NonNull String groupField, @NonNull Object 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 Expr values (Field, FunctionExpression, 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 Expr.alias

Example:

// Get a list of unique genres.
firestore.pipeline().collection("books")
.distinct("genre");
Parameters
@NonNull String groupField

The String representing field name.

@NonNull Object additionalGroups

The Selectable expressions to consider when determining distinct value combinations or Strings representing field names.

Returns
@NonNull Pipeline

A new Pipeline object with this stage appended to the stage list.

execute

public final @NonNull Task<@NonNull Pipeline.Snapshotexecute()

Executes this pipeline and returns the results as a Task of Snapshot.

Returns
@NonNull Task<@NonNull Pipeline.Snapshot>

A Task that will be resolved with the results of the pipeline.

execute

public final @NonNull Task<@NonNull Pipeline.Snapshotexecute(@NonNull Pipeline.ExecuteOptions options)

Executes this pipeline and returns the results as a Task of Snapshot.

Parameters
@NonNull Pipeline.ExecuteOptions options

The ExecuteOptions to use to instruct Firestore backend execution.

Returns
@NonNull Task<@NonNull Pipeline.Snapshot>

A Task that will be resolved with the results of the pipeline.

findNearest

public final @NonNull Pipeline findNearest(
    @NonNull Field vectorField,
    @NonNull double[] vectorValue,
    @NonNull FindNearestStage.DistanceMeasure distanceMeasure
)

Performs a vector similarity search, ordering the result set by most similar to least similar, and returning the full set in the order of similarity.

Parameters
@NonNull Field vectorField

A Field that contains vector to search on.

@NonNull double[] vectorValue

The DoubleArray that is used to measure the distance from vectorField values in the documents.

@NonNull FindNearestStage.DistanceMeasure distanceMeasure

specifies what type of distance is calculated. when performing the search.

Returns
@NonNull Pipeline

A new Pipeline object with this stage appended to the stage list.

findNearest

public final @NonNull Pipeline findNearest(
    @NonNull String vectorField,
    @NonNull double[] vectorValue,
    @NonNull FindNearestStage.DistanceMeasure distanceMeasure
)

Performs a vector similarity search, ordering the result set by most similar to least similar, and returning the full set in the order of similarity.

Parameters
@NonNull String vectorField

A String specifying the vector field to search on.

@NonNull double[] vectorValue

The DoubleArray that is used to measure the distance from vectorField values in the documents.

@NonNull FindNearestStage.DistanceMeasure distanceMeasure

specifies what type of distance is calculated when performing the search.

Returns
@NonNull Pipeline

A new Pipeline object with this stage appended to the stage list.

findNearest

public final @NonNull Pipeline findNearest(
    @NonNull String vectorField,
    @NonNull Expression vectorValue,
    @NonNull FindNearestStage.DistanceMeasure distanceMeasure,
    @NonNull FindNearestOptions options
)

Performs a vector similarity search, ordering the result set by most similar to least similar, and returning the first N documents (specified by FindNearestOptions.withLimit) in the result set.

Example:

// Find books with similar "topicVectors" to the given targetVector
firestore.pipeline().collection("books")
.findNearest("topicVectors", targetVector, FindNearest.DistanceMeasure.COSINE,
new FindNearestOptions()
.withLimit(10)
.withDistanceField("distance"));
Parameters
@NonNull String vectorField

A field name that contains vector to search on.

@NonNull Expression vectorValue

The Expression that should evaluate to a vector used to measure the distance from vectorField values in the documents.

@NonNull FindNearestStage.DistanceMeasure distanceMeasure

specifies what type of distance is calculated when performing the search.

@NonNull FindNearestOptions options

The FindNearestOptions to use when performing the search.

Returns
@NonNull Pipeline

A new Pipeline object with this stage appended to the stage list.

limit

public final @NonNull Pipeline limit(int 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 offset 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 the results to the top 10 highest-rated books
firestore.pipeline().collection("books")
.sort(field("rating").descending())
.limit(10);
Parameters
int limit

The maximum number of documents to return.

Returns
@NonNull Pipeline

A new Pipeline object with this stage appended to the stage list.

offset

public final @NonNull Pipeline offset(int 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 limit to control the size of each page.

Example:

// 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
Parameters
int offset

The number of documents to skip.

Returns
@NonNull Pipeline

A new Pipeline object with this stage appended to the stage list.

rawStage

public final @NonNull Pipeline rawStage(@NonNull RawStage rawStage)

Adds a raw stage to the pipeline by specifying the stage name as an argument. This does not offer any type safety on the stage params and requires the caller to know the order (and optionally names) of parameters accepted by the stage.

This method provides a way to call stages that are supported by the Firestore backend but that are not implemented in the SDK version being used.

Parameters
@NonNull RawStage rawStage

A RawStage object that specifies stage name and parameters.

Returns
@NonNull Pipeline

A new Pipeline object with this stage appended to the stage list.

removeFields

public final @NonNull Pipeline removeFields(@NonNull Field field, @NonNull Field additionalFields)

Remove fields from outputs of previous stages.

Example:

firestore.pipeline().collection("books")
.removeFields(
field("rating"), field("cost")
);
Parameters
@NonNull Field field

The first Field to remove.

@NonNull Field additionalFields

Optional additional Fields to remove.

Returns
@NonNull Pipeline

A new Pipeline object with this stage appended to the stage list.

removeFields

public final @NonNull Pipeline removeFields(@NonNull String field, @NonNull String additionalFields)

Remove fields from outputs of previous stages.

Example:

firestore.pipeline().collection("books")
.removeFields(
"rating", "cost"
);
Parameters
@NonNull String field

The first String name of field to remove.

@NonNull String additionalFields

Optional additional String name of fields to remove.

Returns
@NonNull Pipeline

A new Pipeline object with this stage appended to the stage list.

replaceWith

public final @NonNull Pipeline replaceWith(@NonNull String field)

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:

// 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"
// }
Parameters
@NonNull String field

The String specifying the field name containing the nested map.

Returns
@NonNull Pipeline

A new Pipeline object with this stage appended to the stage list.

replaceWith

public final @NonNull Pipeline replaceWith(@NonNull Expression mapValue)

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:

// Input.
// {
// "name": "John Doe Jr.",
// "parents": {
// "father": "John Doe Sr.",
// "mother": "Jane Doe"
// }

// Emit parents as document.
firestore.pipeline().collection("people").replaceWith(field("parents"));

// Output
// {
// "father": "John Doe Sr.",
// "mother": "Jane Doe"
// }
Parameters
@NonNull Expression mapValue

The Expression or Field containing the nested map.

Returns
@NonNull Pipeline

A new Pipeline object with this stage appended to the stage list.

sample

public final @NonNull Pipeline sample(int documents)

Performs a pseudo-random sampling of the input documents.

The documents parameter represents the target number of documents to produce and must be a non-negative integer value. If the previous stage produces less than size documents, the entire previous results are returned. If the previous stage produces more than size, this outputs a sample of exactly size entries where any sample is equally likely.

Example:

// Sample 10 books, if available.
firestore.pipeline().collection("books")
.sample(10);
Parameters
int documents

The number of documents to emit.

Returns
@NonNull Pipeline

A new Pipeline object with this stage appended to the stage list.

sample

public final @NonNull Pipeline sample(@NonNull SampleStage sample)

Performs a pseudo-random sampling of the input documents.

Examples:

// Sample 10 books, if available.
firestore.pipeline().collection("books")
.sample(Sample.withDocLimit(10));

// Sample 50% of books.
firestore.pipeline().collection("books")
.sample(Sample.withPercentage(0.5));
Parameters
@NonNull SampleStage sample

An SampleStage object that specifies how sampling is performed.

Returns
@NonNull Pipeline

A new Pipeline object with this stage appended to the stage list.

select

public final @NonNull Pipeline select(@NonNull String fieldName, @NonNull Object 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: Reference to an existing field.

  • AliasedExpression: Represents the result of a expression with an assigned alias name using Expr.alias

If no selections are provided, the output of this stage is empty. Use Pipeline.addFields instead if only additions are desired.

Example:

firestore.collection("books")
.select("name", "address");

// The above is a shorthand of this:
firestore.pipeline().collection("books")
.select(field("name"), field("address"));
Parameters
@NonNull String fieldName

The first field to include in the output documents, specified as a string value representing a field names.

@NonNull Object additionalSelections

Optional additional fields to include in the output documents, specified as Selectable expressions or string values representing field names.

Returns
@NonNull Pipeline

A new Pipeline object with this stage appended to the stage list.

select

public final @NonNull Pipeline select(@NonNull Selectable selection, @NonNull Object additionalSelections)

Selects or creates a set of fields from the outputs of previous stages.

The selected fields are defined using Selectable expressions or strings, which can be:

  • String: Name of an existing field

  • Field: Reference to an existing field.

  • AliasedExpression: Represents the result of a expression with an assigned alias name using Expr.alias

If no selections are provided, the output of this stage is empty. Use Pipeline.addFields instead if only additions are desired.

Example:

firestore.pipeline().collection("books")
.select(
field("name"),
field("address").toUppercase().as("upperAddress"),
);
Parameters
@NonNull Selectable selection

The first field to include in the output documents, specified as a Selectable expression.

@NonNull Object additionalSelections

Optional additional fields to include in the output documents, specified as Selectable expressions or string values representing field names.

Returns
@NonNull Pipeline

A new Pipeline object with this stage appended to the stage list.

sort

public final @NonNull Pipeline sort(@NonNull Ordering order, @NonNull Ordering additionalOrders)

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 books by rating in descending order, and then by title in ascending order for books with the same rating
firestore.pipeline().collection("books")
.sort(
Ordering.of("rating").descending(),
Ordering.of("title") // Ascending order is the default
);
Parameters
@NonNull Ordering order

The first Ordering instance specifying the sorting criteria.

@NonNull Ordering additionalOrders

Optional additional Ordering instances specifying the sorting criteria.

Returns
@NonNull Pipeline

A new Pipeline object with this stage appended to the stage list.

union

public final @NonNull Pipeline union(@NonNull Pipeline other)

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:

// Emit documents from books collection and magazines collection.
firestore.pipeline().collection("books")
.union(firestore.pipeline().collection("magazines"));
Parameters
@NonNull Pipeline other

The other Pipeline that is part of union.

Returns
@NonNull Pipeline

A new Pipeline object with this stage appended to the stage list.

unnest

public final @NonNull Pipeline unnest(@NonNull Selectable arrayWithAlias)

Takes a specified array from the input documents and outputs a document for each element with the element stored in a field with name specified by the alias.

For each document emitted by the prior stage, this stage will emit zero or more augmented documents. The input array is found in parameter arrayWithAlias, which can be an Expr with an alias specified via Expr.alias, or a Field that can also have alias specified. For each element of the input array, an augmented document will be produced. The element of input array will be stored in a field with name specified by the alias of the arrayWithAlias parameter. If the arrayWithAlias is a Field with no alias, then the original array field will be replaced with the individual element.

Parameters
@NonNull Selectable arrayWithAlias

The input array with field alias to store output element of array.

Returns
@NonNull Pipeline

A new Pipeline object with this stage appended to the stage list.

unnest

public final @NonNull Pipeline unnest(@NonNull UnnestStage unnestStage)

Takes a specified array from the input documents and outputs a document for each element with the element stored in a field with name specified by the alias.

For each document emitted by the prior stage, this stage will emit zero or more augmented documents. The input array specified in the unnestStage parameter will for each element of the input array produce an augmented document. The element of the input array will be stored in a field with a name specified by the unnestStage parameter.

Optionally, an index field can also be added to emitted documents. See UnnestStage for further information.

Parameters
@NonNull UnnestStage unnestStage

An UnnestStage object that specifies the search parameters.

Returns
@NonNull Pipeline

A new Pipeline object with this stage appended to the stage list.

unnest

public final @NonNull Pipeline unnest(@NonNull String arrayField, @NonNull String alias)

Takes a specified array from the input documents and outputs a document for each element with the element stored in a field with name specified by the alias.

For each document emitted by the prior stage, this stage will emit zero or more augmented documents. The input array found in the previous stage document field specified by the arrayField parameter, will for each element of the input array produce an augmented document. The element of the input array will be stored in a field with name specified by alias parameter on the augmented document.

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("tags", "tag");

// Output:
// { "title": "The Hitchhiker's Guide to the Galaxy", "tag": "comedy", ... }
// { "title": "The Hitchhiker's Guide to the Galaxy", "tag": "space", ... }
// { "title": "The Hitchhiker's Guide to the Galaxy", "tag": "adventure", ... }
Parameters
@NonNull String arrayField

The name of the field containing the array.

@NonNull String alias

The name of field to store emitted element of array.

Returns
@NonNull Pipeline

A new Pipeline object with this stage appended to the stage list.

unnest

public final @NonNull Pipeline unnest(@NonNull Selectable arrayWithAlias, @NonNull UnnestOptions options)

Takes a specified array from the input documents and outputs a document for each element with the element stored in a field with name specified by the alias.

For each document emitted by the prior stage, this stage will emit zero or more augmented documents. The input array is found in parameter arrayWithAlias, which can be an Expr with an alias specified via Expr.alias, or a Field that can also have alias specified. For each element of the input array, an augmented document will be produced. The element of input array will be stored in a field with name specified by the alias of the arrayWithAlias parameter. If the arrayWithAlias is a Field with no alias, then the original array field will be replaced with the individual element.

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("tags", "tag", new UnnestOptions().withIndexField("tagIndex"));

// Output:
// { "title": "The Hitchhiker's Guide to the Galaxy", "tagIndex": 0, "tag": "comedy", ... }
// { "title": "The Hitchhiker's Guide to the Galaxy", "tagIndex": 1, "tag": "space", ... }
// { "title": "The Hitchhiker's Guide to the Galaxy", "tagIndex": 2, "tag": "adventure", ... }
Parameters
@NonNull Selectable arrayWithAlias

The input array with field alias to store output element of array.

@NonNull UnnestOptions options

The UnnestOptions to use when performing the unnest.

Returns
@NonNull Pipeline

A new Pipeline object with this stage appended to the stage list.

where

public final @NonNull Pipeline where(@NonNull BooleanExpression 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: Expr.equal, Expr.lessThan, Expr.greaterThan, etc.

  • logical operators: Expr.and, Expr.or, Expr.not, etc.

  • advanced functions: Expr.regexMatch, Expr.arrayContains, etc.

Example:

firestore.pipeline().collection("books")
.where(
and(
gt("rating", 4.0), // Filter for ratings greater than 4.0
field("genre").equal("Science Fiction") // Equivalent to equal("genre", "Science Fiction")
)
);
Parameters
@NonNull BooleanExpression condition

The BooleanExpression to apply.

Returns
@NonNull Pipeline

A new Pipeline object with this stage appended to the stage list.