匯總
所有匯總函式都可以在 aggregate(...) 階段中做為頂層運算式使用。
| 名稱 | 說明 |
COUNT
|
傳回文件數量。 |
COUNT_IF
|
傳回運算式評估結果為 TRUE 的文件數 |
COUNT_DISTINCT
|
傳回不重複的非 NULL 值數量
|
SUM
|
傳回所有 NUMERIC 值的總和
|
AVERAGE
|
傳回所有 NUMERIC 值的平均值
|
MINIMUM
|
傳回非 NULL 的最小值
|
MAXIMUM
|
傳回非 NULL 的最大值
|
FIRST
|
傳回第一個文件的 expression 值。
|
LAST
|
傳回最後一個文件的 expression 值。
|
ARRAY_AGG
|
傳回所有輸入值的陣列。 |
ARRAY_AGG_DISTINCT
|
傳回所有不重複的輸入值所組成的陣列。 |
COUNT
語法:
count() -> INT64
count(expression: ANY) -> INT64
說明:
傳回先前階段的文件計數,其中 expression 評估為任何非 NULL 值。如未提供 expression,則會傳回前一階段的文件總數。
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() )
COUNT_IF
語法:
count_if(expression: BOOLEAN) -> INT64
說明:
傳回先前階段中,expression 評估為 TRUE 的文件數量。
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() )
COUNT_DISTINCT
語法:
count_distinct(expression: ANY) -> INT64
說明:
傳回 expression 的不重複非 NULL、非 ABSENT 值數量。
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() )
SUM
語法:
sum(expression: ANY) -> NUMBER
說明:
傳回所有數值的總和,並忽略非數值。如果任何值為 NaN,則傳回 NaN。
輸出內容的型別會與最寬的輸入型別相同,但下列情況除外:
- 如果
INTEGER無法表示為INTEGER,系統會將其轉換為DOUBLE。
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() )
AVERAGE
語法:
average(expression: ANY) -> FLOAT64
說明:
傳回所有數值的平均值,並忽略非數值。
如果任何值為 NaN,則評估結果為 NaN;如果沒有任何數值匯總,則評估結果為 NULL。
輸出內容的型別會與輸入型別相同,但下列情況除外:
- 如果
INTEGER無法表示為INTEGER,系統會將其轉換為DOUBLE。
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() )
最低
語法:
minimum(expression: ANY) -> ANY
說明:
針對每個文件評估 expression 時,傳回非 NULL 和非缺少的最小值。
如果沒有非 NULL、非缺少的任何值,則會傳回 NULL。包括未考慮任何文件的情況。
如果有多個最小值,系統可以傳回其中一個值。值類型排序方式遵循文件排序方式。
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() )
MAXIMUM
語法:
maximum(expression: ANY) -> ANY
說明:
針對每個文件評估 expression 時,傳回 expression 的最大非 NULL 值 (非缺席值)。
如果沒有非 NULL、非缺少的任何值,則會傳回 NULL。包括未考慮任何文件的情況。
如果有多個最大等值,則可以傳回其中任何一個值。值類型排序方式遵循文件排序方式。
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() )
FIRST
語法:
first(expression: ANY) -> ANY
說明:
傳回第一個傳回的文件中 expression 的值。
LAST
語法:
last(expression: ANY) -> ANY
說明:
傳回最後一個回傳文件的 expression 值。
ARRAY_AGG
語法:
array_agg(expression: ANY) -> ARRAY<ANY>
說明:
針對每份文件評估時,傳回包含所有 expression 值的陣列。
如果運算式解析為缺席值,則會轉換為 NULL。
輸出陣列中的元素順序不穩定,不應依賴。
ARRAY_AGG_DISTINCT
語法:
array_agg_distinct(expression: ANY) -> ARRAY<ANY>
說明:
針對每份文件評估時,傳回包含所有不重複 expression 值的陣列。
如果運算式解析為缺席值,則會轉換為 NULL。
輸出陣列中的元素順序不穩定,不應依賴。