Referenz zu Aggregatfunktionen
Aggregat
Alle Aggregatfunktionen können in der Phase aggregate(...) als Ausdrücke der obersten Ebene verwendet werden.
| Name | Beschreibung |
COUNT
|
Gibt die Anzahl der Dokumente zurück. |
COUNT_IF
|
Gibt die Anzahl der Dokumente zurück, in denen ein Ausdruck als TRUE ausgewertet wird.
|
COUNT_DISTINCT
|
Gibt die Anzahl der eindeutigen Nicht-NULL-Werte zurück.
|
SUM
|
Gibt die Summe aller NUMERIC-Werte zurück.
|
AVERAGE
|
Gibt den Durchschnitt aller NUMERIC-Werte zurück.
|
MINIMUM
|
Gibt den Mindestwert ungleich NULL zurück.
|
MAXIMUM
|
Gibt den Höchstwert ungleich NULL zurück.
|
FIRST
|
Gibt den Wert von expression für das erste Dokument zurück.
|
LAST
|
Gibt den Wert von expression für das letzte Dokument zurück.
|
ARRAY_AGG
|
Gibt ein Array aller Eingabewerte zurück. |
ARRAY_AGG_DISTINCT
|
Gibt ein Array aller eindeutigen Eingabewerte zurück. |
COUNT
Syntax:
count() -> INT64
count(expression: ANY) -> INT64
Beschreibung:
Gibt die Anzahl der Dokumente aus der vorherigen Phase zurück, in denen expression als ein beliebiger Wert ungleich NULL ausgewertet wird. Wenn kein expression angegeben ist, wird die Gesamtzahl der Dokumente aus der vorherigen Phase zurückgegeben.
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
Android
// 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
Android
// 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();
Go
// 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
Syntax:
count_if(expression: BOOLEAN) -> INT64
Beschreibung:
Gibt die Anzahl der Dokumente aus der vorherigen Phase zurück, in denen expression als TRUE ausgewertet wird.
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") .aggregate([ AggregateFunction("count_if", [Field("rating").greaterThan(4)]).as("filteredCount") ]) .execute()
Kotlin
Android
val result = db.pipeline() .collection("books") .aggregate( AggregateFunction.countIf(field("rating").greaterThan(4)).alias("filteredCount") ) .execute()
Java
Android
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();
Go
snapshot := client.Pipeline(). Collection("books"). Aggregate(firestore.Accumulators( firestore.CountIf(firestore.FieldOf("rating").GreaterThan(4)).As("filteredCount"), )). Execute(ctx)
COUNT_DISTINCT
Syntax:
count_distinct(expression: ANY) -> INT64
Beschreibung:
Gibt die Anzahl der eindeutigen Werte von expression zurück, die nicht NULL und nicht ABSENT sind.
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") .aggregate([AggregateFunction("count_distinct", [Field("author")]).as("unique_authors")]) .execute()
Kotlin
Android
val result = db.pipeline() .collection("books") .aggregate(AggregateFunction.countDistinct("author").alias("unique_authors")) .execute()
Java
Android
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();
Go
snapshot := client.Pipeline(). Collection("books"). Aggregate(firestore.Accumulators( firestore.CountDistinct("author").As("unique_authors"), )). Execute(ctx)
SUMME
Syntax:
sum(expression: ANY) -> NUMBER
Beschreibung:
Gibt die Summe aller numerischen Werte zurück und ignoriert nicht numerische Werte. Gibt NaN zurück, wenn einer der Werte NaN ist.
Die Ausgabe hat denselben Typ wie der breiteste Eingabetyp, außer in den folgenden Fällen:
- Ein
INTEGERwird in einenDOUBLEkonvertiert, wenn er nicht alsINTEGERdargestellt werden kann.
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
Android
val result = db.pipeline() .collection("cities") .aggregate(AggregateFunction.sum("population").alias("totalPopulation")) .execute()
Java
Android
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();
Go
snapshot := client.Pipeline(). Collection("cities"). Aggregate(firestore.Accumulators( firestore.Sum("population").As("totalPopulation"), )). Execute(ctx)
MITTELWERT
Syntax:
average(expression: ANY) -> FLOAT64
Beschreibung:
Gibt den Durchschnitt aller numerischen Werte zurück und ignoriert nicht numerische Werte.
Wird als NaN ausgewertet, wenn einer der Werte NaN ist, oder als NULL, wenn keine numerischen Werte aggregiert werden.
Die Ausgabe hat denselben Typ wie der Eingabetyp, außer in den folgenden Fällen:
- Ein
INTEGERwird in einenDOUBLEkonvertiert, wenn er nicht alsINTEGERdargestellt werden kann.
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
Android
val result = db.pipeline() .collection("cities") .aggregate(AggregateFunction.average("population").alias("averagePopulation")) .execute()
Java
Android
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();
Go
snapshot := client.Pipeline(). Collection("cities"). Aggregate(firestore.Accumulators( firestore.Average("population").As("averagePopulation"), )). Execute(ctx)
MINIMUM
Syntax:
minimum(expression: ANY) -> ANY
Beschreibung:
Gibt den Mindestwert von expression zurück, der nicht NULL und nicht „absent“ ist, wenn er für jedes Dokument ausgewertet wird.
Wenn keine Werte vorhanden sind, die nicht NULL und nicht „absent“ sind, wird NULL zurückgegeben. Das gilt auch, wenn keine Dokumente berücksichtigt werden.
Wenn mehrere gleichwertige Mindestwerte vorhanden sind, kann einer dieser Werte zurückgegeben werden. Die Sortierung der Werttypen folgt der dokumentierten Sortierung.
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
Android
val result = db.pipeline() .collection("books") .aggregate(AggregateFunction.minimum("price").alias("minimumPrice")) .execute()
Java
Android
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();
Go
snapshot := client.Pipeline(). Collection("books"). Aggregate(firestore.Accumulators( firestore.Minimum("price").As("minimumPrice"), )). Execute(ctx)
MAXIMUM
Syntax:
maximum(expression: ANY) -> ANY
Beschreibung:
Gibt den Höchstwert von expression zurück, der nicht NULL und nicht „absent“ ist, wenn er für jedes Dokument ausgewertet wird.
Wenn keine Werte vorhanden sind, die nicht NULL und nicht „absent“ sind, wird NULL zurückgegeben. Das gilt auch, wenn keine Dokumente berücksichtigt werden.
Wenn mehrere gleichwertige Höchstwerte vorhanden sind, kann einer dieser Werte zurückgegeben werden. Die Sortierung der Werttypen folgt der dokumentierten Sortierung.
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
Android
val result = db.pipeline() .collection("books") .aggregate(AggregateFunction.maximum("price").alias("maximumPrice")) .execute()
Java
Android
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();
Go
snapshot := client.Pipeline(). Collection("books"). Aggregate(firestore.Accumulators( firestore.Maximum("price").As("maximumPrice"), )). Execute(ctx)
FIRST
Syntax:
first(expression: ANY) -> ANY
Beschreibung:
Gibt den Wert von expression für das erste zurückgegebene Dokument zurück.
LAST
Syntax:
last(expression: ANY) -> ANY
Beschreibung:
Gibt den Wert von expression für das letzte zurückgegebene Dokument zurück.
ARRAY_AGG
Syntax:
array_agg(expression: ANY) -> ARRAY<ANY>
Beschreibung:
Gibt ein Array mit allen Werten von expression zurück, wenn er für jedes Dokument ausgewertet wird.
Wenn der Ausdruck in einen Wert „absent“ aufgelöst wird, wird er in NULL konvertiert.
Die Reihenfolge der Elemente im Ausgabearray ist nicht stabil und sollte nicht berücksichtigt werden.
ARRAY_AGG_DISTINCT
Syntax:
array_agg_distinct(expression: ANY) -> ARRAY<ANY>
Beschreibung:
Gibt ein Array mit allen eindeutigen Werten von expression zurück, wenn er für jedes Dokument ausgewertet wird.
Wenn der Ausdruck in einen Wert „absent“ aufgelöst wird, wird er in NULL konvertiert.
Die Reihenfolge der Elemente im Ausgabearray ist nicht stabil und sollte nicht berücksichtigt werden.
Nächste Schritte
- Übersicht über Pipelineabfragen ansehen