在火店查询中实现 OR - 火库火店



我正在尝试在火店查询中实现逻辑 OR 运算符。

db.collection('users').where('company_id', '==', companyId)
                          .where('role', '==', 'Maker')
                          .where('role', '==', 'Checker')
                          .where('role', '==', 'Approver')
                          .get().then(function(adminsSnapshot){

             //Some processing on dataSnapshot
})

但这不是实现 OR 的正确方法。

我希望所有用户都具有"制作者、检查者或审批者"角色。

我将如何在消防店查询中实现OR?文档中没有任何内容。

编辑(2019 年 11 月)Cloud Firestore 现在支持"IN"查询(公告),它允许您执行一种类型的 OR 查询,以查找同一字段上具有几个值之一的文档。

例如,对于上面的查询:

db.collection('users')
  .where('company_id', '==', companyId)
  .where('role', 'in', ['Maker', 'Checker', 'Approver']);
<小时 />

原答案

Cloud Firestore 中没有"OR"查询。 如果要在单个查询中实现此目的,则需要一个字段,例如 maker_or_checker_or_approver: true

当然,您始终可以执行三个查询并在客户端上加入它们。

现在,

通过新添加的对in运算符和array-contains-any运算符的支持,在 Firestore 中可以做到这一点,这些运算符允许在单个查询中查询多达 10 个值。

https://firebase.googleblog.com/2019/11/cloud-firestore-now-supports-in-queries.html

因此,使用您的示例,查询将如下所示。

db.collection('users')
    .where('company_id', '==', companyId)
    .where('role', 'in', ['Maker', 'Checker', 'Approver']);

在上面的示例中,如果数据存储在数组中,请将in替换为array-contains-any

FTI OR查询现已在预览:)中提供

来源: https://cloud.google.com/firestore/docs/release-notes#March_24_2023

你可以使用 rxjs 组合可观察量, 参见下面的 angular(6) 示例;

orQuery(){
    const $one = this.afs.collection("users", ref => ref.where("company_id","==","companyId")).valueChanges();
    const $two = this.afs.collection("users", ref => ref.where("role","==","Maker")).valueChanges();
    return combineLatest($one,$two).pipe(
        map(([one, two]) => [...one, ...two])
    )
}
getOr(){
    this.orQuery().subscribe(data => console.log(data))
}

最新更新