Funkcje agregujące

Zbiorcze

Wszystkie funkcje agregujące mogą być używane jako wyrażenia najwyższego poziomu na etapie aggregate(...).

Nazwa Opis
COUNT Zwraca liczbę dokumentów.
COUNT_IF Zwraca liczbę dokumentów, w których wyrażenie ma wartość TRUE
COUNT_DISTINCT Zwraca liczbę unikalnych wartości innych niż NULL
SUM Zwraca sumę wszystkich wartości NUMERIC
AVERAGE Zwraca średnią wszystkich wartości NUMERIC
MINIMUM Zwraca minimalną wartość inną niż NULL
MAXIMUM Zwraca maksymalną wartość inną niż NULL
FIRST Zwraca wartość expression dla pierwszego dokumentu.
LAST Zwraca wartość expression dla ostatniego dokumentu.
ARRAY_AGG Zwraca tablicę wszystkich wartości wejściowych.
ARRAY_AGG_DISTINCT Zwraca tablicę wszystkich unikalnych wartości wejściowych.

COUNT

Składnia:

count() -> INT64
count(expression: ANY) -> INT64

Opis:

Zwraca liczbę dokumentów z poprzedniego etapu, w których expression zwraca dowolną wartość inną niż NULL. Jeśli nie podasz parametru expression, zwracana jest łączna liczba dokumentów z poprzedniego etapu.

Node.js
// Total number of books in the collection
const countOfAll = await db.pipeline()
  .collection("books")
  .aggregate(countAll().as("count"))
  .execute();

// Number of books with nonnull `ratings` field
const countField = await db.pipeline()
  .collection("books")
  .aggregate(field("ratings").count().as("count"))
  .execute();

Web

// Total number of books in the collection
const countOfAll = await execute(db.pipeline()
  .collection("books")
  .aggregate(countAll().as("count"))
);

// Number of books with nonnull `ratings` field
const countField = await execute(db.pipeline()
  .collection("books")
  .aggregate(field("ratings").count().as("count"))
);
Swift
// Total number of books in the collection
let countAll = try await db.pipeline()
  .collection("books")
  .aggregate([CountAll().as("count")])
  .execute()

// Number of books with nonnull `ratings` field
let countField = try await db.pipeline()
  .collection("books")
  .aggregate([Field("ratings").count().as("count")])
  .execute()

Kotlin

// Total number of books in the collection
val countAll = db.pipeline()
    .collection("books")
    .aggregate(AggregateFunction.countAll().alias("count"))
    .execute()

// Number of books with nonnull `ratings` field
val countField = db.pipeline()
    .collection("books")
    .aggregate(AggregateFunction.count("ratings").alias("count"))
    .execute()

Java

// Total number of books in the collection
Task<Pipeline.Snapshot> countAll = db.pipeline()
    .collection("books")
    .aggregate(AggregateFunction.countAll().alias("count"))
    .execute();

// Number of books with nonnull `ratings` field
Task<Pipeline.Snapshot> countField = db.pipeline()
    .collection("books")
    .aggregate(AggregateFunction.count("ratings").alias("count"))
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Count

# Total number of books in the collection
count_all = (
    client.pipeline().collection("books").aggregate(Count().as_("count")).execute()
)

# Number of books with nonnull `ratings` field
count_field = (
    client.pipeline()
    .collection("books")
    .aggregate(Count("ratings").as_("count"))
    .execute()
)
Java
// Total number of books in the collection
Pipeline.Snapshot countAll =
    firestore.pipeline().collection("books").aggregate(countAll().as("count")).execute().get();

// Number of books with nonnull `ratings` field
Pipeline.Snapshot countField =
    firestore
        .pipeline()
        .collection("books")
        .aggregate(count("ratings").as("count"))
        .execute()
        .get();
Go
// Total number of books in the collection
countAll, err := client.Pipeline().Collection("books").
	Aggregate(firestore.Accumulators(firestore.CountAll().As("count"))).
	Execute(ctx).Results().GetAll()
if err != nil {
	fmt.Fprintf(w, "GetAll failed: %v", err)
	return err
}

// Number of books with nonnull `ratings` field
countField, err := client.Pipeline().
	Collection("books").
	Aggregate(firestore.Accumulators(firestore.Count("ratings").As("count"))).
	Execute(ctx).Results().GetAll()
if err != nil {
	fmt.Fprintf(w, "GetAll failed: %v", err)
	return err
}

COUNT_IF

Składnia:

count_if(expression: BOOLEAN) -> INT64

Opis:

Zwraca liczbę dokumentów z poprzedniego etapu, w których expression ma wartość TRUE.

Node.js
const result = await db.pipeline()
  .collection("books")
  .aggregate(
    field("rating").greaterThan(4).countIf().as("filteredCount")
  )
  .execute();

Web

const result = await execute(db.pipeline()
  .collection("books")
  .aggregate(
    field("rating").greaterThan(4).countIf().as("filteredCount")
  )
);
Swift
let result = try await db.pipeline()
  .collection("books")
  .aggregate([
    AggregateFunction("count_if", [Field("rating").greaterThan(4)]).as("filteredCount")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("books")
    .aggregate(
        AggregateFunction.countIf(field("rating").greaterThan(4)).alias("filteredCount")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("books")
    .aggregate(
        AggregateFunction.countIf(field("rating").greaterThan(4)).alias("filteredCount")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("books")
    .aggregate(Field.of("rating").greater_than(4).count_if().as_("filteredCount"))
    .execute()
)
Java
Pipeline.Snapshot result =
    firestore
        .pipeline()
        .collection("books")
        .aggregate(countIf(field("rating").greaterThan(4)).as("filteredCount"))
        .execute()
        .get();
Go
snapshot := client.Pipeline().
	Collection("books").
	Aggregate(firestore.Accumulators(
		firestore.CountIf(firestore.FieldOf("rating").GreaterThan(4)).As("filteredCount"),
	)).
	Execute(ctx)

COUNT_DISTINCT

Składnia:

count_distinct(expression: ANY) -> INT64

Opis:

Zwraca liczbę unikalnych wartości expression, które nie są równe NULL ani ABSENT.

Node.js
const result = await db.pipeline()
  .collection("books")
  .aggregate(field("author").countDistinct().as("unique_authors"))
  .execute();

Web

const result = await execute(db.pipeline()
  .collection("books")
  .aggregate(field("author").countDistinct().as("unique_authors"))
);
Swift
let result = try await db.pipeline()
  .collection("books")
  .aggregate([AggregateFunction("count_distinct", [Field("author")]).as("unique_authors")])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("books")
    .aggregate(AggregateFunction.countDistinct("author").alias("unique_authors"))
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("books")
    .aggregate(AggregateFunction.countDistinct("author").alias("unique_authors"))
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("books")
    .aggregate(Field.of("author").count_distinct().as_("unique_authors"))
    .execute()
)
Java
Pipeline.Snapshot result =
    firestore
        .pipeline()
        .collection("books")
        .aggregate(countDistinct("author").as("unique_authors"))
        .execute()
        .get();
Go
snapshot := client.Pipeline().
	Collection("books").
	Aggregate(firestore.Accumulators(
		firestore.CountDistinct("author").As("unique_authors"),
	)).
	Execute(ctx)

SUMA

Składnia:

sum(expression: ANY) -> NUMBER

Opis:

Zwraca sumę wszystkich wartości liczbowych, ignorując wartości nieliczbowe. Zwraca NaN, jeśli dowolne wartości to NaN.

Dane wyjściowe będą miały taki sam typ jak najszerszy typ danych wejściowych, z wyjątkiem tych przypadków:

  • Jeśli nie można przedstawić wartości INTEGER jako INTEGER, zostanie ona przekonwertowana na DOUBLE.
Node.js
const result = await db.pipeline()
  .collection("cities")
  .aggregate(field("population").sum().as("totalPopulation"))
  .execute();

Web

const result = await execute(db.pipeline()
  .collection("cities")
  .aggregate(field("population").sum().as("totalPopulation"))
);
Swift
let result = try await db.pipeline()
  .collection("cities")
  .aggregate([Field("population").sum().as("totalPopulation")])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("cities")
    .aggregate(AggregateFunction.sum("population").alias("totalPopulation"))
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("cities")
    .aggregate(AggregateFunction.sum("population").alias("totalPopulation"))
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("cities")
    .aggregate(Field.of("population").sum().as_("totalPopulation"))
    .execute()
)
Java
Pipeline.Snapshot result =
    firestore
        .pipeline()
        .collection("cities")
        .aggregate(sum("population").as("totalPopulation"))
        .execute()
        .get();
Go
snapshot := client.Pipeline().
	Collection("cities").
	Aggregate(firestore.Accumulators(
		firestore.Sum("population").As("totalPopulation"),
	)).
	Execute(ctx)

ŚREDNIA

Składnia:

average(expression: ANY) -> FLOAT64

Opis:

Zwraca średnią wszystkich wartości liczbowych, ignorując wartości nieliczbowe. Zwraca wartość NaN, jeśli dowolne wartości to NaN, lub NULL, jeśli nie są agregowane żadne wartości liczbowe.

Dane wyjściowe będą miały taki sam typ jak dane wejściowe, z wyjątkiem tych przypadków:

  • Jeśli nie można przedstawić wartości INTEGER jako INTEGER, zostanie ona przekonwertowana na DOUBLE.
Node.js
const result = await db.pipeline()
  .collection("cities")
  .aggregate(field("population").average().as("averagePopulation"))
  .execute();

Web

const result = await execute(db.pipeline()
  .collection("cities")
  .aggregate(field("population").average().as("averagePopulation"))
);
Swift
let result = try await db.pipeline()
  .collection("cities")
  .aggregate([Field("population").average().as("averagePopulation")])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("cities")
    .aggregate(AggregateFunction.average("population").alias("averagePopulation"))
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("cities")
    .aggregate(AggregateFunction.average("population").alias("averagePopulation"))
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("cities")
    .aggregate(Field.of("population").average().as_("averagePopulation"))
    .execute()
)
Java
Pipeline.Snapshot result =
    firestore
        .pipeline()
        .collection("cities")
        .aggregate(average("population").as("averagePopulation"))
        .execute()
        .get();
Go
snapshot := client.Pipeline().
	Collection("cities").
	Aggregate(firestore.Accumulators(
		firestore.Average("population").As("averagePopulation"),
	)).
	Execute(ctx)

MINIMUM

Składnia:

minimum(expression: ANY) -> ANY

Opis:

Zwraca minimalną wartość expression, która nie jest NULL ani nie jest nieobecna, po ocenie każdego dokumentu.

Jeśli nie ma wartości innych niż NULL i nieobecne, zwracana jest wartość NULL. Dotyczy to również sytuacji, gdy nie są uwzględniane żadne dokumenty.

Jeśli istnieje kilka minimalnych równoważnych wartości, może zostać zwrócona dowolna z nich. Kolejność typów wartości jest zgodna z udokumentowaną kolejnością.

Node.js
const result = await db.pipeline()
  .collection("books")
  .aggregate(field("price").minimum().as("minimumPrice"))
  .execute();

Web

const result = await execute(db.pipeline()
  .collection("books")
  .aggregate(field("price").minimum().as("minimumPrice"))
);
Swift
let result = try await db.pipeline()
  .collection("books")
  .aggregate([Field("price").minimum().as("minimumPrice")])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("books")
    .aggregate(AggregateFunction.minimum("price").alias("minimumPrice"))
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("books")
    .aggregate(AggregateFunction.minimum("price").alias("minimumPrice"))
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("books")
    .aggregate(Field.of("price").minimum().as_("minimumPrice"))
    .execute()
)
Java
Pipeline.Snapshot result =
    firestore
        .pipeline()
        .collection("books")
        .aggregate(minimum("price").as("minimumPrice"))
        .execute()
        .get();
Go
snapshot := client.Pipeline().
	Collection("books").
	Aggregate(firestore.Accumulators(
		firestore.Minimum("price").As("minimumPrice"),
	)).
	Execute(ctx)

MAKSYMALNA

Składnia:

maximum(expression: ANY) -> ANY

Opis:

Zwraca maksymalną wartość expression, która nie jest wartością NULL ani wartością nieobecną, po ocenie każdego dokumentu.

Jeśli nie ma wartości innych niż NULL i nieobecne, zwracana jest wartość NULL. Dotyczy to również sytuacji, gdy nie są uwzględniane żadne dokumenty.

Jeśli istnieje wiele maksymalnych równoważnych wartości, może zostać zwrócona dowolna z nich. Kolejność typów wartości jest zgodna z udokumentowaną kolejnością.

Node.js
const result = await db.pipeline()
  .collection("books")
  .aggregate(field("price").maximum().as("maximumPrice"))
  .execute();

Web

const result = await execute(db.pipeline()
  .collection("books")
  .aggregate(field("price").maximum().as("maximumPrice"))
);
Swift
let result = try await db.pipeline()
  .collection("books")
  .aggregate([Field("price").maximum().as("maximumPrice")])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("books")
    .aggregate(AggregateFunction.maximum("price").alias("maximumPrice"))
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("books")
    .aggregate(AggregateFunction.maximum("price").alias("maximumPrice"))
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("books")
    .aggregate(Field.of("price").maximum().as_("maximumPrice"))
    .execute()
)
Java
Pipeline.Snapshot result =
    firestore
        .pipeline()
        .collection("books")
        .aggregate(maximum("price").as("maximumPrice"))
        .execute()
        .get();
Go
snapshot := client.Pipeline().
	Collection("books").
	Aggregate(firestore.Accumulators(
		firestore.Maximum("price").As("maximumPrice"),
	)).
	Execute(ctx)

FIRST

Składnia:

first(expression: ANY) -> ANY

Opis:

Zwraca wartość expression dla pierwszego zwróconego dokumentu.

OSTATNI

Składnia:

last(expression: ANY) -> ANY

Opis:

Zwraca wartość expression dla ostatniego zwróconego dokumentu.

ARRAY_AGG

Składnia:

array_agg(expression: ANY) -> ARRAY<ANY>

Opis:

Zwraca tablicę zawierającą wszystkie wartości expression po ocenie każdego dokumentu.

Jeśli wyrażenie zwraca brak wartości, jest ono konwertowane na NULL.

Kolejność elementów w tablicy wyjściowej nie jest stała i nie należy na niej polegać.

ARRAY_AGG_DISTINCT

Składnia:

array_agg_distinct(expression: ANY) -> ARRAY<ANY>

Opis:

Zwraca tablicę zawierającą wszystkie różne wartości expression po ocenie każdego dokumentu.

Jeśli wyrażenie zwraca brak wartości, jest ona konwertowana na NULL.

Kolejność elementów w tablicy wyjściowej nie jest stała i nie należy na niej polegać.