คำอธิบาย
สร้างเอกสารใหม่โดยอ้างอิงชุดย่อยของช่องที่มีอยู่ หรือกำหนดช่องให้กับผลลัพธ์ของนิพจน์ที่กำหนด
ตัวอย่าง
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();