Aggregate
אפשר להשתמש בכל הפונקציות המצטברות כביטויים ברמה העליונה בשלב 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(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.
סדר הרכיבים במערך הפלט לא יציב, ואין להסתמך עליו.