How to perform compound queries with logical OR in Cloud Firestore?

后端 未结 9 1678
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 02:07

From the docs:

You can also chain multiple where() methods to create more specific queries (logical AND).

How can I perform an <

相关标签:
9条回答
  • 2020-11-22 02:36

    OR isn't supported as it's hard for the server to scale it (requires keeping state to dedup). The work around is to issue 2 queries, one for each condition, and dedup on the client.


    Edit (Nov 2019):

    Cloud Firestore now supports IN queries which are a limited type of OR query.

    For the example above you could do:

    // Get all documents in 'foo' where status is open or upcmoming
    db.collection('foo').where('status','in',['open','upcoming']).get()
    

    However it's still not possible to do a general OR condition involving multiple fields.

    0 讨论(0)
  • 2020-11-22 02:36

    you can bind two Observables using the rxjs merge operator. Here you have an example.

    import { Observable } from 'rxjs/Observable';
    import 'rxjs/add/observable/merge';
    
    ...
    
    getCombinatedStatus(): Observable<any> {
       return Observable.merge(this.db.collection('foo', ref => ref.where('status','==','open')).valueChanges(),
                               this.db.collection('foo', ref => ref.where('status','==','upcoming')).valueChanges());
    }
    

    Then you can subscribe to the new Observable updates using the above method:

    getCombinatedStatus.subscribe(results => console.log(results);
    

    I hope this can help you, greetings from Chile!!

    0 讨论(0)
  • 2020-11-22 02:40

    I would have no "status" field, but status related fields, updating them to true or false based on request, like

    { name: "a", status_open: true, status_upcoming: false, status_closed: false}
    

    However, check Firebase Cloud Functions. You could have a function listening status changes, updating status related properties like

    { name: "a", status: "open", status_open: true, status_upcoming: false, status_closed: false}
    

    one or the other, your query could be just

    ...where('status_open','==',true)...
    

    Hope it helps.

    0 讨论(0)
提交回复
热议问题