설명
다른 파이프라인의 문서를 현재 파이프라인의 문서와 병합합니다.
구문
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() )
자바
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() )
자바
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 단계를 추가할 수 있습니다.