集計
すべての集計関数は、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(expression: ANY) -> ANY
説明:
各ドキュメントで評価された、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 に変換されます。
出力配列内の要素の順序は不安定なため、それに依存すべきではありません。