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&q"count")) .execute();test.firestore.js
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(&quocount().as("count")) );test.firestore.js
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&q;count")]) .execute()PipelineSnippets.swift
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("ratins("count")) .execute()DocSnippets.kt
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 T>askPipeline.Snapshot countField = db.pipeline() .collection("books") .aggregate(AggregateFunction.count("ratingquot;count")) .execute();DocSnippets.java
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("ratinunt")) .execute() )firestore_pipelines.py
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(&quo .execute() .get();PipelineSnippets.java
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("filtered .execute();test.firestore.js
Web
const result = await execute(db.pipeline() .collection("books") .aggregate( field("rating").greaterThan(4).countIf().as(&ququot;) ) );test.firestore.js
Swift
let result = try await db.pipeline() .collection("books") .aggregate([ AggregateFunction("count_if", [Field("rating").greaterThan(4)]).as(&quo ]) .execute()PipelineSnippets.swift
Kotlin
val result = db.pipeline() .collection("books") .aggregate( AggregateFunction.countIf(field("rating").greaterThan(4)).alias("filteredCou) .execute()DocSnippets.kt
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .aggregate( AggregateFunction.countIf(field("rating").greaterThan(4)).alias("filteredCoun .execute();DocSnippets.java
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_("filteredcute() )firestore_pipelines.py
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .aggregate(countIf(field("rating").greaterThan(4)).as("filteredCount")) .get();PipelineSnippets.java
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 .execute();test.firestore.js
Web
const result = await execute(db.pipeline() .collection("books") .aggregate(field("author").countDistinct().as(&rs")) );test.firestore.js
Swift
let result = try await db.pipeline() .collection("books") .aggregate([AggregateFunction("count_distinct", [Field("author")]).as(&qt;)]) .execute()PipelineSnippets.swift
Kotlin
val result = db.pipeline() .collection("books") .aggregate(AggregateFunction.countDistinct("author").alias("unique_) .execute()DocSnippets.kt
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .aggregate(AggregateFunction.countDistinct("author").alias("unique_a .execute();DocSnippets.java
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .aggregate(Field.of("author").count_distinct().as_("unique_aucute() )firestore_pipelines.py
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .aggregate(countDistinct("author").as("unique_authors")) .get();PipelineSnippets.java
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
INTEGERnie można przedstawić jakoINTEGER, zostanie ona przekonwertowana naDOUBLE.
Node.js
const result = await db.pipeline() .collection("cities") .aggregate(field("population").sum().as("totalPo .execute();test.firestore.js
Web
const result = await execute(db.pipeline() .collection("cities") .aggregate(field("population").sum().as(&qon")) );test.firestore.js
Swift
let result = try await db.pipeline() .collection("cities") .aggregate([Field("population").sum().as("totalPoxecute()PipelineSnippets.swift
Kotlin
val result = db.pipeline() .collection("cities") .aggregate(AggregateFunction.sum("population").alias("totalPop) .execute()DocSnippets.kt
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("cities") .aggregate(AggregateFunction.sum("population").alias("totalPopu .execute();DocSnippets.java
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("cities") .aggregate(Field.of("population").sum().as_("totalPopulcute() )firestore_pipelines.py
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("cities") .aggregate(sum("population").as("totalPopulation")) .get();PipelineSnippets.java
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
INTEGERnie można przedstawić jakoINTEGER, zostanie ona przekonwertowana naDOUBLE.
Node.js
const result = await db.pipeline() .collection("cities") .aggregate(field("population").average().as("averagePo .execute();test.firestore.js
Web
const result = await execute(db.pipeline() .collection("cities") .aggregate(field("population").average().as(&quoon")) );test.firestore.js
Swift
let result = try await db.pipeline() .collection("cities") .aggregate([Field("population").average().as("averagePoxecute()PipelineSnippets.swift
Kotlin
val result = db.pipeline() .collection("cities") .aggregate(AggregateFunction.average("population").alias("averagePop) .execute()DocSnippets.kt
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("cities") .aggregate(AggregateFunction.average("population").alias("averagePopu .execute();DocSnippets.java
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("cities") .aggregate(Field.of("population").average().as_("averagePopulcute() )firestore_pipelines.py
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("cities") .aggregate(average("population").as("averagePopulation")) .get();PipelineSnippets.java
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("mini .execute();test.firestore.js
Web
const result = await execute(db.pipeline() .collection("books") .aggregate(field("price").minimum().asce")) );test.firestore.js
Swift
let result = try await db.pipeline() .collection("books") .aggregate([Field("price").minimum().as("minixecute()PipelineSnippets.swift
Kotlin
val result = db.pipeline() .collection("books") .aggregate(AggregateFunction.minimum("price").alias("minim) .execute()DocSnippets.kt
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .aggregate(AggregateFunction.minimum("price").alias("minimu .execute();DocSnippets.java
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .aggregate(Field.of("price").minimum().as_("minimumcute() )firestore_pipelines.py
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .aggregate(minimum("price").as("minimumPrice")) .get();PipelineSnippets.java
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("maxi .execute();test.firestore.js
Web
const result = await execute(db.pipeline() .collection("books") .aggregate(field("price").maximum().asce")) );test.firestore.js
Swift
let result = try await db.pipeline() .collection("books") .aggregate([Field("price").maximum().as("maxixecute()PipelineSnippets.swift
Kotlin
val result = db.pipeline() .collection("books") .aggregate(AggregateFunction.maximum("price").alias("maxim) .execute()DocSnippets.kt
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .aggregate(AggregateFunction.maximum("price").alias("maximu .execute();DocSnippets.java
Python
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .aggregate(Field.of("price").maximum().as_("maximumcute() )firestore_pipelines.py
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .aggregate(maximum("price").as("maximumPrice")) .get();PipelineSnippets.java
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ć.