תיאור
יצירת מסמכים חדשים, או על ידי הפניה לקבוצת משנה של שדות קיימים, או על ידי הקצאת שדה לתוצאה של ביטוי נתון.
דוגמאות
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();
התנהגות
המיקום של שלב בחירה
אין הגבלות על השימוש בשלב בחירה, אבל לא תהיה גישה לשדות שלא נכללים בשלב בחירה בשלבים הבאים בצינור. לדוגמה, כדי לבחור רק את השדות 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();