توضیحات
اسناد جدید را تولید میکند، یا با ارجاع به زیرمجموعهای از فیلدهای موجود، یا با اختصاص یک فیلد به نتیجهی یک عبارت داده شده.
مثالها
Web
const result = await execute(db.pipeline() .collection("books") .select(field("soldBooks").multiply(field("price")).round().as("partialRevenue")) .aggregate(field("partialRevenue").sum().as("totalRevenue")) );
سویفت
let result = try await db.pipeline() .collection("books") .select([Field("soldBooks").multiply(Field("price")).round().as("partialRevenue")]) .aggregate([Field("partialRevenue").sum().as("totalRevenue")]) .execute()
Kotlin
val result = db.pipeline() .collection("books") .select(Expression.multiply(field("soldBooks"), field("price")).round().alias("partialRevenue")) .aggregate(AggregateFunction.sum("partialRevenue").alias("totalRevenue")) .execute()
Java
Task<Pipeline.Snapshot> result = db.pipeline() .collection("books") .select(Expression.multiply(field("soldBooks"), field("price")).round().alias("partialRevenue")) .aggregate(AggregateFunction.sum("partialRevenue").alias("totalRevenue")) .execute();
پایتون
from google.cloud.firestore_v1.pipeline_expressions import Field result = ( client.pipeline() .collection("books") .select( Field.of("soldBooks") .multiply(Field.of("price")) .round() .as_("partialRevenue") ) .aggregate(Field.of("partialRevenue").sum().as_("totalRevenue")) .execute() )
جاوا
Pipeline.Snapshot result = firestore .pipeline() .collection("books") .select(round(multiply(field("soldBooks"), field("price"))).as("partialRevenue")) .aggregate(sum("partialRevenue").as("totalRevenue")) .execute() .get();
برو
snapshot := client.Pipeline(). Collection("books"). Select(firestore.Fields( firestore.Round(firestore.Multiply(firestore.FieldOf("soldBooks"), firestore.FieldOf("price"))).As("partialRevenue"), )). Aggregate(firestore.Accumulators( firestore.Sum("partialRevenue").As("totalRevenue"), )). Execute(ctx)
رفتار
موقعیت یک مرحله انتخابی
هیچ محدودیتی در مورد زمان استفاده از یک مرحله انتخاب وجود ندارد، اما هر فیلدی که در یک مرحله انتخاب گنجانده نشده باشد، برای مراحل بعدی در یک خط لوله قابل دسترسی نخواهد بود. به عنوان مثال، برای انتخاب فقط فیلدهای name و location همه شهرهای کانادا از مجموعه داده زیر:
نود جی اس
await db.collection("cities").doc("SF").set({
name: "San Francisco",
population: 800000,
location: {country: "USA", state: "California"}
});
await db.collection("cities").doc("TO").set({
name: "Toronto",
population: 3000000,
location: {country: "Canada", province: "Ontario"}
});
میتوان از لوله زیر استفاده کرد:
نود جی اس
const names = await db.pipeline()
.collection("/cities")
.where(equal(field("location.country"), "Canada"))
.select(stringConcat(field("name"), ", ", field("location.country")).as("name"), "population")
.execute();
که اسناد زیر را تولید میکند:
{ name: "Toronto, Canada", population: 3000000 },
با این حال، اگر مرحله select(...) قبل از مرحله where(...) قرار گیرد، مانند:
نود جی اس
const names = await db.pipeline()
.collection("/cities")
.select(stringConcat(field("name"), ",", field("location.country")).as("name"), "population")
.where(equal(field("location.country"), "Canada"))
.execute();
هیچ سندی تولید نخواهد شد، زیرا location.country قبل از اجرای مرحله where(...) از سند حذف شده است.
انتخاب فیلدهای تو در تو
مرحله select(...) میتواند برای انتخاب فیلدهای تو در تو از نقشهها و آرایهها استفاده شود. به عنوان مثال، برای انتخاب فیلد country تو در تو و اولین ورودی آرایه landmarks از اسناد زیر:
نود جی اس
await db.collection("cities").doc("SF").set({
name: "San Francisco",
population: 800000,
location: { country: "USA", state: "California" },
landmarks: [ "Golden Gate Bridge", "Alcatraz" ]
});
await db.collection("cities").doc("TO").set({
name: "Toronto",
population: 3000000,
province: "ON",
location: { country: "Canada", province: "Ontario" },
landmarks: [ "CN Tower", "Casa Loma" ]
});
await db.collection("cities").doc("AT").set({
name: "Atlantis",
population: null
});
میتوان از لوله زیر استفاده کرد:
نود جی اس
const locations = await db.pipeline()
.collection("/cities")
.select(
field("name").as("city"),
field("location.country").as("country"),
field("landmarks").offset(0).as("topLandmark"))
.execute();
که اسناد زیر را تولید میکند:
{ city: "San Francisco", country: "USA", topLandmark: "Golden Gate Bridge" },
{ city: "Toronto", country: "Canada", topLandmark: "CN Tower" },
{ city: "Atlantis" }
اگر یک مقدار map یا مقدار آرایه تودرتو وجود نداشته باشد، در سند حاصل گنجانده نمیشود. دسترسی به آرایه و map در مرحله select به ترتیب مشابه توابع offset(...) و get_field(...) عمل میکند.
اختصاص فیلدهای تو در تو
نتیجه یک عبارت همچنین میتواند به یک فیلد تودرتو اختصاص داده شود، که این امر امکان بازگرداندن زیرمجموعهای از فیلدهای تودرتو از مرحله قبل را در مرحله select(...) فراهم میکند. به عنوان مثال، میتوان از کد زیر برای اطمینان از بازگرداندن فقط اطلاعات city و state و در عین حال حفظ شکل اصلی سند استفاده کرد:
نود جی اس
const results = await db.pipeline()
.collection("/users")
.addFields(
field("__name__"),
field("address.city").as("address.city"),
field("address.state").as("address.state"))
.execute();