Union (Transformation Stage)
Preview — Firestore in Native mode (with Pipeline Operations) for Enterprise Edition
This feature is subject to the "Pre-GA Offerings Terms" in the General Service Terms section of theService Specific Terms. You can process personal data for this feature as outlined in theCloud Data Processing Addendum, subject to the obligations and restrictions described in the agreement under which you access Google Cloud. Pre-GA features are available "as is" and might have limited support. For more information, see thelaunch stage descriptions.
Description
Merges the documents from another pipeline with those in the current pipeline.
Syntax
Node.js
constresults=awaitdb.pipeline().collection("cities/SF/restaurants").union(db.pipeline().collection("cities/NYC/restaurants")).execute();Client examples
Node.js
constresults=awaitdb.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
constresults=awaitexecute(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
letresults=tryawaitdb.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
Android
valresults=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
Android
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
fromgoogle.cloud.firestore_v1.pipeline_expressionsimportFieldresults=(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.Snapshotresults=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();
Behavior
This stage runs multiple pipelines in parallel and concatenates the results together.
Non-Deterministic Order of Results
The order in which results are combined between the two pipelines isnon-deterministic. Any perceived order is unstable and shouldn't be relied upon. A followingsort stage can be added if a stable order is required.
Node.js
constresults=awaitdb.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
Android
valresults=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
Android
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
fromgoogle.cloud.firestore_v1.pipeline_expressionsimportFieldresults=(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.Snapshotresults=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();
Duplicate Results
Theunion stage does not deduplicate results. A followingdistinct oraggregate stage can be added if duplicate results need to be removed.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-02-18 UTC.