คำอธิบาย
ผสานเอกสารจากไปป์ไลน์อื่นกับเอกสารในไปป์ไลน์ปัจจุบัน
ไวยากรณ์
Node.js
const results = await db.pipeline()
.collection("cities/SF/restaurants")
.union(db.pipeline().collection("cities/NYC/restaurants"))
.execute();
ตัวอย่างไคลเอ็นต์
Node.js
const results = await db.pipeline() .collection("cities/SF/restaurants") .where(eq("type", "chinese")) .union(db.pipeline() .collection("cities/NYC/restaurants") .where(eq("type", "italian"))) .where(gte("rating", 4.5)) .execute();
Web
const results = await execute(db.pipeline() .collection("cities/SF/restaurants") .where(field("type").equal("Chinese")) .union(db.pipeline() .collection("cities/NY/restaurants") .where(field("type").equal("Italian"))) .where(field("rating").greaterThanOrEqual(4.5)) .sort(field("__name__").descending()) );
Swift
let results = try await db.pipeline() .collection("cities/SF/restaurants") .where(Field("type").equal("Chinese")) .union(with: db.pipeline() .collection("cities/NY/restaurants") .where(Field("type").equal("Italian"))) .where(Field("rating").greaterThanOrEqual(4.5)) .sort([Field("__name__").descending()]) .execute()
Kotlin
val results = db.pipeline() .collection("cities/SF/restaurants") .where(field("type").equal("Chinese")) .union(db.pipeline() .collection("cities/NY/restaurants") .where(field("type").equal("Italian"))) .where(field("rating").greaterThanOrEqual(4.5)) .sort(field("__name__").descending()) .execute()
Java
Task<Pipeline.Snapshot> results = db.pipeline() .collection("cities/SF/restaurants") .where(field("type").equal("Chinese")) .union(db.pipeline() .collection("cities/NY/restaurants") .where(field("type").equal("Italian"))) .where(field("rating").greaterThanOrEqual(4.5)) .sort(field("__name__").descending()) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field results = ( client.pipeline() .collection("cities/SF/restaurants") .where(Field.of("type").equal("Chinese")) .union( client.pipeline() .collection("cities/NY/restaurants") .where(Field.of("type").equal("Italian")) ) .where(Field.of("rating").greater_than_or_equal(4.5)) .sort(Field.of("__name__").descending()) .execute() )
Java
Pipeline.Snapshot results = firestore .pipeline() .collection("cities/SF/restaurants") .where(field("type").equal("Chinese")) .union( firestore .pipeline() .collection("cities/NY/restaurants") .where(field("type").equal("Italian"))) .where(field("rating").greaterThanOrEqual(4.5)) .sort(descending(field("__name__"))) .execute() .get();
พฤติกรรม
ขั้นตอนนี้จะเรียกใช้ไปป์ไลน์หลายรายการแบบคู่ขนานและต่อผลลัพธ์เข้าด้วยกัน
ลำดับผลลัพธ์ที่ไม่แน่นอน
ลำดับการรวมผลลัพธ์ระหว่าง 2 ไปป์ไลน์นั้น
ไม่แน่นอน ลำดับที่รับรู้ได้ไม่เสถียรและไม่ควรนำมาใช้ คุณเพิ่มsortขั้นตอนต่อไปนี้ได้หากต้องการลำดับที่เสถียร
Node.js
const results = await db.pipeline() .collection("cities/SF/restaurants") .where(eq("type", "chinese")) .union(db.pipeline() .collection("cities/NYC/restaurants") .where(eq("type", "italian"))) .where(gte("rating", 4.5)) .sort(Field.of("__name__")) .execute();
Kotlin
val results = db.pipeline() .collection("cities/SF/restaurants") .where(field("type").equal("Chinese")) .union(db.pipeline() .collection("cities/NY/restaurants") .where(field("type").equal("Italian"))) .where(field("rating").greaterThanOrEqual(4.5)) .sort(field("__name__").descending()) .execute()
Java
Task<Pipeline.Snapshot> results = db.pipeline() .collection("cities/SF/restaurants") .where(field("type").equal("Chinese")) .union(db.pipeline() .collection("cities/NY/restaurants") .where(field("type").equal("Italian"))) .where(field("rating").greaterThanOrEqual(4.5)) .sort(field("__name__").descending()) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field results = ( client.pipeline() .collection("cities/SF/restaurants") .where(Field.of("type").equal("Chinese")) .union( client.pipeline() .collection("cities/NY/restaurants") .where(Field.of("type").equal("Italian")) ) .where(Field.of("rating").greater_than_or_equal(4.5)) .sort(Field.of("__name__").descending()) .execute() )
Java
Pipeline.Snapshot results = firestore .pipeline() .collection("cities/SF/restaurants") .where(field("type").equal("Chinese")) .union( firestore .pipeline() .collection("cities/NY/restaurants") .where(field("type").equal("Italian"))) .where(field("rating").greaterThanOrEqual(4.5)) .sort(descending(field("__name__"))) .execute() .get();
ผลการค้นหาที่ซ้ำกัน
union ขั้นตอนจะไม่กรองผลลัพธ์ที่ซ้ำกัน คุณเพิ่มขั้นตอนdistinctหรือaggregateได้หากต้องการนำผลการค้นหาที่ซ้ำกันออก