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

ProblemsOfSumit picture ProblemsOfSumit · Oct 8, 2017 · Viewed 11.2k times · Source

From the docs:

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

How can I perform an OR query? Example:

  1. Give me all documents where the field status is open OR upcoming
  2. Give me all documents where the field status == open OR createdAt <= <somedatetime>

Answer

Dan McGrath picture Dan McGrath · Oct 8, 2017

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.