0

I'm doing a web application using Angular 8 and Angularfire. I'm trying to query a collection using two where with ==, but, it's not working.

I was following the official guide of querying-collection from Angularfire.

Using this code there is no results at all:

getChats(): Observable<IChatRoom[]> {
    return this.afs.collection<IChatRoom>('chats', ref => {
      let query: firebase.firestore.CollectionReference | firebase.firestore.Query = ref;
      query = query.where('state', '==', 'open');
      query = query.where('state', '==', 'transferred');
      return query;
    }).valueChanges();
  }

It works only with one where, like this:

getChats(): Observable<IChatRoom[]> {
    return this.afs.collection<IChatRoom>('chats', ref => {
      let query: firebase.firestore.CollectionReference | firebase.firestore.Query = ref;
      query = query.where('state', '==', 'open');
      return query;
    }).valueChanges();
  }

I have also tried:

getChats(): Observable<IChatRoom[]> {
    return this.afs.collection<IChatRoom>('chats', ref =>
      ref
        .where('state', '==', 'open')
        .where('state', '==', 'transferred')
    ).valueChanges();
  }

My goal is to get all documents in a collection that have the state field equal to open or transferred.

RRGT19
  • 1,437
  • 3
  • 28
  • 54
  • 1
    Multiple where clauses are always logically AND'd together. You can't use them to logically OR their conditions. Use an "IN" query to check for multiple values in a single field. – Doug Stevenson Mar 01 '20 at 18:56
  • @DougStevenson Thanks for the clarification, now it's working. I didn't know about the existence of the `in`, it should be mentioned in the querying-collection from Angularfire. – RRGT19 Mar 01 '20 at 19:03
  • Great point. Feel like filing a bug on the [AngularFire2 repo](https://github.com/angular/angularfire/blob/master/docs/firestore/querying-collections.md) (and maybe a PR too of how you'd like to see it fixed)? – Frank van Puffelen Mar 01 '20 at 19:23

0 Answers0