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 przyjmuje 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 przyjmuje wartość inną niż NULL. Jeśli nie podano expression, zwraca łączną liczbę 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();

COUNT_IF

Składnia:

count_if(expression: BOOLEAN) -> INT64

Opis:

Zwraca liczbę dokumentów z poprzedniego etapu, w których expression przyjmuje 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();

COUNT_DISTINCT

Składnia:

count_distinct(expression: ANY) -> INT64

Opis:

Zwraca liczbę unikalnych wartości expression innych niż NULL i 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();

SUM

Składnia:

sum(expression: ANY) -> NUMBER

Opis:

Zwraca sumę wszystkich wartości liczbowych, ignorując wartości nieliczbowe. Zwraca NaN, jeśli któraś z wartości to NaN.

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

  • Jeśli wartości INTEGER nie można przedstawić 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();

AVERAGE

Składnia:

average(expression: ANY) -> FLOAT64

Opis:

Zwraca średnią wszystkich wartości liczbowych, ignorując wartości nieliczbowe. Przyjmuje wartość NaN, jeśli któraś z wartości to NaN, lub NULL, jeśli nie ma zagregowanych wartości liczbowych.

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

  • Jeśli wartości INTEGER nie można przedstawić 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();

MINIMUM

Składnia:

minimum(expression: ANY) -> ANY

Opis:

Zwraca minimalną wartość expression inną niż NULL i absent, gdy jest ona obliczana w każdym dokumencie.

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

Jeśli jest kilka minimalnych wartości równoważnych, można zwrócić dowolną 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();

MAXIMUM

Składnia:

maximum(expression: ANY) -> ANY

Opis:

Zwraca maksymalną wartość expression inną niż NULL i absent, gdy jest ona obliczana w każdym dokumencie.

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

Jeśli jest kilka maksymalnych wartości równoważnych, można zwrócić dowolną 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();

FIRST

Składnia:

first(expression: ANY) -> ANY

Opis:

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

LAST

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 obliczone w każdym dokumencie.

Jeśli wyrażenie przyjmuje wartość absent, jest ona konwertowana na NULL.

Kolejność elementów w tablicy wyjściowej nie jest stabilna 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 unikalne wartości expression obliczone w każdym dokumencie.

Jeśli wyrażenie przyjmuje wartość absent, jest ona konwertowana na NULL.

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