Funkcje agregujące

Zbiorcze

Wszystkie funkcje agregacji 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 pierwszego dokumentu.
LAST Zwraca wartość expression ostatniego dokumentu.
ARRAY_AGG Zwraca tablicę wszystkich wartości wejściowych.
ARRAY_AGG_DISTINCT Zwraca tablicę wszystkich niepowtarzalnych 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 ma wartość inną niż NULL. Jeśli nie podasz expression, zwróci łą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();

LICZ.JEŻELI

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 innych niż NULL i ABSENT w expression.

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();

SUMA

Składnia:

sum(expression: ANY) -> NUMBER

Opis:

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

Typ danych wyjściowych będzie taki sam jak typ danych wejściowych o największym zakresie, z wyjątkiem tych przypadków:

  • Jeśli znaku INTEGER nie można przedstawić jako INTEGER, zostanie on przekonwertowany 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();

ŚREDNIA

Składnia:

average(expression: ANY) -> FLOAT64

Opis:

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

Typ danych wyjściowych będzie taki sam jak typ danych wejściowych, z wyjątkiem tych przypadków:

  • Jeśli znaku INTEGER nie można przedstawić jako INTEGER, zostanie on przekonwertowany 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, która nie jest wartością NULL ani wartością pustą, po obliczeniu dla każdego dokumentu.

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

Jeśli istnieje 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();

MAKSYMALNA

Składnia:

maximum(expression: ANY) -> ANY

Opis:

Zwraca maksymalną wartość inną niż NULL i nieobecną w przypadku expression po obliczeniu dla każdego dokumentu.

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

Jeśli istnieje 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.

OSTATNI

Składnia:

last(expression: ANY) -> ANY

Opis:

Zwraca wartość expression w przypadku ostatniego zwróconego dokumentu.

ARRAY_AGG

Składnia:

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

Opis:

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

Jeśli wyrażenie przyjmuje wartość nieobecną, jest ona konwertowana 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 unikalne wartości expression po obliczeniu dla każdego dokumentu.

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

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