Union

说明

将另一个流水线中的文档与当前流水线中的文档合并。

语法

Node.js

const results = await db.pipeline()
  .collection("cities/SF/restaurants")
  .union(db.pipeline().collection("cities/NYC/restaurants"))
  .execute();

行为

此阶段并行运行多个流水线,并将结果串联在一起。

结果的非确定性顺序

两个流水线之间合并结果的顺序是不确定的。任何感知到的顺序都是不稳定的,不应依赖。如果需要稳定的顺序,可以添加后续的 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()
)

重复结果

union 阶段不会对结果进行去重。如果需要移除重复结果,可以添加后续的 distinctaggregate 阶段。

更多示例

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()
)