בחירה

תיאור

יוצר מסמכים חדשים, על ידי הפניה לקבוצת משנה של שדות קיימים או על ידי הקצאת שדה לתוצאה של ביטוי נתון.

דוגמאות

Web

const result = await execute(db.pipeline()
  .collection("books")
  .select(field("soldBooks").multiply(field("price")).round().as("partialRevenue"))
  .aggregate(field("partialRevenue").sum().as("totalRevenue"))
  );
Swift
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();
Python
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()
)
Java
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)

התנהגות

שדות מטא-נתונים של מסמכים

השלב select(...) פולט רק את השדות או הביטויים שצוינו במפורש בשלב. זה שונה מפעולות ליבה, שבהן ההגדרה select(...) בשאילתה פועלת כמסכת מאפיינים ולא כהיטל, וגם תמיד כוללת את שדות המטא-נתונים של המסמך __name__,‏ __create_time__ ו-__update_time__.

כדי לקבל התנהגות בסגנון של פעולות ליבה ב-select(...), צריך לציין את השדות באופן מפורש, למשל:

Node.js

const results = await db.pipeline()
  .collection("/users")
  .select(
    field("__name__"),
    field("__create_time__"),
    field("__update_time__"),
    field("email"))
  .execute();

המיקום של שלב נבחר

אין הגבלות על השימוש בשלב select, אבל כל השדות שלא נכללים בשלב select לא יהיו נגישים לשלבים הבאים בצינור עיבוד הנתונים. לדוגמה, כדי לבחור רק את השדות name ו-location של כל הערים בקנדה מקבוצת הנתונים הבאה:

Node.js

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"}
});

אפשר להשתמש בצינור עיבוד הנתונים הבא:

Node.js

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(...), למשל:

Node.js

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 מהמסמכים הבאים:

Node.js

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
});

אפשר להשתמש בצינור עיבוד הנתונים הבא:

Node.js

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" }

אם ערך של מפה או מערך בתצוגת עץ לא קיים, הוא לא נכלל במסמך שנוצר. הגישה למערך ולמפה בשלב הבחירה פועלת באופן זהה לפונקציות offset(...) ו-get_field(...), בהתאמה.

הקצאת שדות בתוך שדות

אפשר גם להקצות את התוצאה של ביטוי לשדה בתצוגת עץ, וכך השלב select(...) יכול להחזיר קבוצת משנה של שדות בתצוגת עץ מהשלב הקודם. לדוגמה, אפשר להשתמש בפרמטרים הבאים כדי לוודא שיוחזרו רק הפרטים של city ו-state, תוך שמירה על הצורה המקורית של המסמך:

Node.js

const results = await db.pipeline()
  .collection("/users")
  .addFields(
    field("__name__"),
    field("address.city").as("address.city"),
    field("address.state").as("address.state"))
  .execute();