توضیحات
تمام اسناد موجود در یک پایگاه داده را در مجموعههای مختلف و سطوح تو در تو برمیگرداند.
مثالها
Web
// Count all documents in the database const results = await execute(db.pipeline() .database() .aggregate(countAll().as("total")) );
سویفت
// Count all documents in the database let results = try await db.pipeline() .database() .aggregate([CountAll().as("total")]) .execute()
Kotlin
// Count all documents in the database val results = db.pipeline() .database() .aggregate(AggregateFunction.countAll().alias("total")) .execute()
Java
// Count all documents in the database Task<Pipeline.Snapshot> results = db.pipeline() .database() .aggregate(AggregateFunction.countAll().alias("total")) .execute();
پایتون
from google.cloud.firestore_v1.pipeline_expressions import Count # Count all documents in the database results = client.pipeline().database().aggregate(Count().as_("total")).execute()
جاوا
// Count all documents in the database Pipeline.Snapshot results = firestore.pipeline().database().aggregate(countAll().as("total")).execute().get();
برو
// Count all documents in the database snapshot := client.Pipeline(). Database(). Aggregate(firestore.Accumulators(firestore.CountAll().As("total"))). Execute(ctx)
رفتار
برای استفاده از مرحله database(...) ، باید به عنوان اولین مرحله در pipeline ظاهر شود.
ترتیب اسناد برگردانده شده از مرحله database(...) ناپایدار است و نمیتوان به آن اعتماد کرد. مرحله sort(...) بعدی میتواند برای به دست آوردن یک ترتیب قطعی استفاده شود.
به عنوان مثال، برای اسناد زیر:
نود جی اس
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});
مرحله database(...) میتواند برای بازیابی تمام اسناد موجود در پایگاه داده استفاده شود.
نود جی اس
const results = await db.pipeline()
.database()
.sort(field("population").ascending())
.execute();
این پرس و جو اسناد زیر را تولید میکند:
{ name: "San Francsico", state: "California", population: 800000 }
{ name: "California", population: 39000000 }
{ name: "United States of America", population: 340000000 }