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:
- Give me all documents where the field
status
isopen
ORupcoming
- Give me all documents where the field
status == open
ORcreatedAt <= <somedatetime>
With the recent addition of IN queries, Firestore supports "up to 10 equality clauses on the same field with a logical OR"
A possible solution to (1) would be:
See Firebase Guides: Query Operators |
in
andarray-contains-any
I would have no "status" field, but status related fields, updating them to true or false based on request, like
However, check Firebase Cloud Functions. You could have a function listening status changes, updating status related properties like
one or the other, your query could be just
Hope it helps.
We have the same problem just now, luckily the only possible values for ours are A,B,C,D (4) so we have to query for things like A||B, A||C, A||B||C, D, etc
As of like a few months ago firebase supports a new query
array-contains
so what we do is make an array and we pre-process the OR values to the arrayAnd we do this for all
4!
values or however many combos there are.THEN we can simply check the query
[document arrayContains:@"a||c"]
or whatever type of condition we need.So if something only qualified for conditional
A
of our 4 conditionals (A,B,C,D) then its array would contain the following literal strings:@["A", "A||B", "A||C", "A||D", "A||B||C", "A||B||D", "A||C||D", "A||B||C||D"]
Then for any of those
OR
combinations we can just searcharray-contains
on whatever we may want (e.g. "A||C")Note: This is only a reasonable approach if you have a few number of possible values to compare OR with.
More info on Array-contains here, since it's newish to firebase docs
suggest to give value for status as well.
ex.
you can query by
ref.where('statusValue', '<=', 20)
then both'a'
and'b'
will found.this can save your query cost and performance.
btw, it is not fix all case.
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 ofOR
query.For the example above you could do:
However it's still not possible to do a general
OR
condition involving multiple fields.you can bind two Observables using the rxjs merge operator. Here you have an example.
Then you can subscribe to the new Observable updates using the above method:
I hope this can help you, greetings from Chile!!