집계
모든 집계 함수는 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
구문:
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의 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로 변환됩니다.
출력 배열의 요소 순서는 안정적이지 않으므로 이에 의존해서는 안 됩니다.