AggregateStage

public final class AggregateStage extends Stage


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.

Summary

Nested types

public static class AggregateStage.Companion

Public methods

boolean
equals(Object other)
int
static final @NonNull AggregateStage
withAccumulators(
    @NonNull AliasedAggregate accumulator,
    @NonNull AliasedAggregate additionalAccumulators
)

Create AggregateStage with one or more accumulators.

final @NonNull AggregateStage
withGroups(@NonNull Selectable group, @NonNull Object additionalGroups)

Add one or more groups to AggregateStage

final @NonNull AggregateStage
withGroups(@NonNull String groupField, @NonNull Object additionalGroups)

Add one or more groups to AggregateStage

Inherited methods

From com.google.firebase.firestore.pipeline.Stage
final @NonNull AggregateStage
final @NonNull AggregateStage
withOption(@NonNull String key, boolean value)

Specify named Boolean parameter

final @NonNull AggregateStage
withOption(@NonNull String key, double value)

Specify named Double parameter

final @NonNull AggregateStage

Specify named Field parameter

final @NonNull AggregateStage
withOption(@NonNull String key, long value)

Specify named Long parameter

final @NonNull AggregateStage

Specify named String parameter

Public methods

equals

public boolean equals(Object other)

hashCode

public int hashCode()

withAccumulators

public static final @NonNull AggregateStage withAccumulators(
    @NonNull AliasedAggregate accumulator,
    @NonNull AliasedAggregate additionalAccumulators
)

Create AggregateStage with one or more accumulators.

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 AggregateStage

AggregateStage with specified accumulators.

withGroups

public final @NonNull AggregateStage withGroups(@NonNull Selectable group, @NonNull Object additionalGroups)

Add one or more groups to AggregateStage

Parameters
@NonNull Selectable group

The Selectable expression to consider when determining group value combinations.

@NonNull Object additionalGroups

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

Returns
@NonNull AggregateStage

AggregateStage with specified groups.

withGroups

public final @NonNull AggregateStage withGroups(@NonNull String groupField, @NonNull Object additionalGroups)

Add one or more groups to AggregateStage

Parameters
@NonNull String groupField

The String representing field name.

@NonNull Object additionalGroups

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

Returns
@NonNull AggregateStage

AggregateStage with specified groups.