說明
將其他管道的文件與目前管道的文件合併。
語法
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();
行為
這個階段會平行執行多個管道,並將結果串連在一起。
結果順序不確定
兩個管道之間合併結果的順序不確定。任何可辨識的順序都不穩定,不應做為依據。如需穩定排序,可以新增後續 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 階段。