צבירה
אפשר להשתמש בכל פונקציות הצבירה כביטויים ברמה העליונה בשלב 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, הפונקציה מחזירה את המספר הכולל של המסמכים מהשלב הקודם.
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").count().as("count")) .execute();
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() )
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("count")) .execute() .get();
המשך
// Total number of books in the collection countAll, err := client.Pipeline().Collection("books"). Aggregate(firestore.Accumulators(firestore.CountAll().As("count"))). Execute(ctx).Results().GetAll() if err != nil { fmt.Fprintf(w, "GetAll failed: %v", err) return err } // Number of books with nonnull `ratings` field countField, err := client.Pipeline(). Collection("books"). Aggregate(firestore.Accumulators(firestore.Count("ratings").As("count"))). Execute(ctx).Results().GetAll() if err != nil { fmt.Fprintf(w, "GetAll failed: %v", err) return err }
COUNT_IF
תחביר:
count_if(expression: BOOLEAN) -> INT64
תיאור:
הפונקציה מחזירה את מספר המסמכים מהשלב הקודם שבו expression
מוערך כ-TRUE.
Node.js
const result = await db.pipeline() .collection("books") .aggregate( field("rating").greaterThan(4).countIf().as("filteredCount") ) .execute();
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") <<<<<<< HEAD .aggregate([ AggregateFunction(functionName: "count_if", args: [Field("rating").greaterThan(4)]).as("filteredCount") ]) ======= .aggregate([Field("rating").greaterThan(4).countIf().as("filteredCount")]) >>>>>>> master .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() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .aggregate(countIf(field("rating").greaterThan(4)).as("filteredCount")) .execute() .get();
המשך
snapshot := client.Pipeline(). Collection("books"). Aggregate(firestore.Accumulators( firestore.CountIf(firestore.FieldOf("rating").GreaterThan(4)).As("filteredCount"), )). Execute(ctx)
COUNT_DISTINCT
תחביר:
count_distinct(expression: ANY) -> INT64
תיאור:
הפונקציה מחזירה את מספר הערכים הייחודיים של expression שאינם NULL ואינם ABSENT.
Node.js
const result = await db.pipeline() .collection("books") .aggregate(field("author").countDistinct().as("unique_authors")) .execute();
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") <<<<<<< HEAD .aggregate([AggregateFunction(functionName: "count_distinct", args: [Field("author")]).as("unique_authors")]) ======= .aggregate([Field("author").countDistinct().as("unique_authors")]) >>>>>>> master .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() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .aggregate(countDistinct("author").as("unique_authors")) .execute() .get();
המשך
snapshot := client.Pipeline(). Collection("books"). Aggregate(firestore.Accumulators( firestore.CountDistinct("author").As("unique_authors"), )). Execute(ctx)
SUM
תחביר:
sum(expression: ANY) -> NUMBER
תיאור:
הפונקציה מחזירה את הסכום של כל הערכים המספריים, ומתעלמת מערכים לא מספריים. הפונקציה מחזירה את הערך NaN אם אחד מהערכים הוא NaN.
הפלט יהיה מאותו סוג כמו סוג הקלט הרחב ביותר, למעט במקרים הבאים:
- אם אי אפשר לייצג
INTEGERבתורINTEGER, הוא יומר ל-DOUBLE.
Node.js
const result = await db.pipeline() .collection("cities") .aggregate(field("population").sum().as("totalPopulation")) .execute();
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() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("cities") .aggregate(sum("population").as("totalPopulation")) .execute() .get();
המשך
snapshot := client.Pipeline(). Collection("cities"). Aggregate(firestore.Accumulators( firestore.Sum("population").As("totalPopulation"), )). Execute(ctx)
AVERAGE
תחביר:
average(expression: ANY) -> FLOAT64
תיאור:
הפונקציה מחזירה את הממוצע של כל הערכים המספריים, ומתעלמת מערכים לא מספריים.
הפונקציה מחזירה NaN אם אחד מהערכים הוא NaN, או NULL אם לא נצברו ערכים מספריים.
הפלט יהיה מאותו סוג כמו סוג הקלט, חוץ מהמקרים הבאים:
- אם אי אפשר לייצג
INTEGERבתורINTEGER, הוא יומר ל-DOUBLE.
Node.js
const result = await db.pipeline() .collection("cities") .aggregate(field("population").average().as("averagePopulation")) .execute();
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() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("cities") .aggregate(average("population").as("averagePopulation")) .execute() .get();
המשך
snapshot := client.Pipeline(). Collection("cities"). Aggregate(firestore.Accumulators( firestore.Average("population").As("averagePopulation"), )). Execute(ctx)
מינימום
תחביר:
minimum(expression: ANY) -> ANY
תיאור:
הפונקציה מחזירה את הערך המינימלי של expression שאינו NULL או חסר, כשהוא מוערך בכל מסמך.
אם אין ערכים שהם לא NULL או לא חסרים, הפונקציה מחזירה NULL. זה כולל גם מקרים שבהם לא נלקחים בחשבון מסמכים.
אם יש כמה ערכים מינימליים שווים, כל אחד מהערכים האלה יכול להיות מוחזר. סדר סוגי הערכים נקבע לפי הסדר המתועד.
Node.js
const result = await db.pipeline() .collection("books") .aggregate(field("price").minimum().as("minimumPrice")) .execute();
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() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .aggregate(minimum("price").as("minimumPrice")) .execute() .get();
המשך
snapshot := client.Pipeline(). Collection("books"). Aggregate(firestore.Accumulators( firestore.Minimum("price").As("minimumPrice"), )). Execute(ctx)
מקסימום
תחביר:
maximum(expression: ANY) -> ANY
תיאור:
מחזירה את הערך המקסימלי של expression שאינו NULL ואינו חסר, כשהוא מוערך בכל מסמך.
אם אין ערכים שהם לא NULL או לא חסרים, הפונקציה מחזירה NULL. זה כולל גם מקרים שבהם לא נלקחים בחשבון מסמכים.
אם יש כמה ערכים מקסימליים שווים, כל אחד מהם יכול להיות מוחזר. סדר סוגי הערכים נקבע לפי הסדר המתועד.
Node.js
const result = await db.pipeline() .collection("books") .aggregate(field("price").maximum().as("maximumPrice")) .execute();
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() )
Java
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .aggregate(maximum("price").as("maximumPrice")) .execute() .get();
המשך
snapshot := client.Pipeline(). Collection("books"). Aggregate(firestore.Accumulators( firestore.Maximum("price").As("maximumPrice"), )). Execute(ctx)
ראשון
תחביר:
first(expression: ANY) -> ANY
תיאור:
הפונקציה מחזירה את הערך של expression למסמך הראשון שהוחזר.
אחרון
תחביר:
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.
סדר הרכיבים במערך הפלט לא יציב ואי אפשר להסתמך עליו.