Implementing OR in firestore query - Firebase fire

2019-01-04 03:39发布

I am trying implement logical OR operator in firestore query.

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


             //Some processing on dataSnapshot
})

But this is not the right way to implement OR.

I want all users with roles of 'Maker, Checker or Approver'.

How would i implement OR in firestore query ? There's nothing in Doc.

2条回答
The star\"
2楼-- · 2019-01-04 04:03

There is no "OR" query in Cloud Firestore. If you want to achieve this in a single query you will need a single field like maker_or_checker_or_approver: true.

Of course you can always do three queries and join them on the client.

查看更多
beautiful°
3楼-- · 2019-01-04 04:03

You could combine the observables using rxjs, see angular(6) example below;

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))
}
查看更多
登录 后发表回答