Datenbank (Eingabephase)
Beschreibung
Gibt alle Dokumente in einer Datenbank über verschiedene Sammlungen und verschachtelte Ebenen hinweg zurück.
Beispiele
Web
// Count all documents in the database const results = await execute(db.pipeline() .database() .aggregate(countAll().as("total")) );
Swift
// Count all documents in the database let results = try await db.pipeline() .database() .aggregate([CountAll().as("total")]) .execute()
Kotlin
Android
// Count all documents in the database val results = db.pipeline() .database() .aggregate(AggregateFunction.countAll().alias("total")) .execute()
Java
Android
// Count all documents in the database Task<Pipeline.Snapshot> results = db.pipeline() .database() .aggregate(AggregateFunction.countAll().alias("total")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Count # Count all documents in the database results = client.pipeline().database().aggregate(Count().as_("total")).execute()
Java
// Count all documents in the database Pipeline.Snapshot results = firestore.pipeline().database().aggregate(countAll().as("total")).execute().get();
Go
// Count all documents in the database snapshot := client.Pipeline(). Database(). Aggregate(firestore.Accumulators(firestore.CountAll().As("total"))). Execute(ctx)
Verhalten
Damit Sie die Phase database(...) verwenden können, muss sie als erste Phase in der Pipeline aufgeführt sein.
Die Reihenfolge der Dokumente, die in der Phase database(...) zurückgegeben werden, ist instabil und kann nicht als zuverlässig angesehen werden. Mit einer nachfolgenden sort(...)-Phase kann eine deterministische Reihenfolge erreicht werden.
Beispiel:
Node.js
await db.collection("cities").doc("SF").set({name: "San Francsico", state: "California", population: 800000});
await db.collection("states").doc("CA").set({name: "California", population: 39000000});
await db.collection("countries").doc("USA").set({name: "United States of America", population: 340000000});
Mit der Phase database(...) können alle Dokumente in der Datenbank abgerufen werden.
Node.js
const results = await db.pipeline()
.database()
.sort(field("population").ascending())
.execute();
Mit dieser Abfrage werden die folgenden Dokumente erstellt:
{ name: "San Francsico", state: "California", population: 800000 }
{ name: "California", population: 39000000 }
{ name: "United States of America", population: 340000000 }