You can now query Firestore using the OR operator. Your application may often need to compare one or more fields with two or more values and select the documents that match at least one of the values. This is an extension to the IN operator which could be used to compare multiple values with the same field.
For example, if you’re searching for information about cats and dogs, you could use the OR operator to combine the two search terms into a single query:
‘cats = black OR dogs = brown’
This would return results that include information about both black cats and brown dogs.
Firestore now supports OR queries via server and client SDKs, with streaming and offline compatibility.
Use Cases
Support for OR operator now enables you to utilize Firestore for building applications which need to filter one or more fields with two or more values using any of our existing query constructs.
Examples
Let’s say you are running an employee management portal on Firestore and you want the ability to filter on a number of criteria like Title, Organization, Location, Tenure etc., you can now easily utilize OR to be able to do it.
- code_block
- [StructValue([(u’code’, u’CollectionReference collection = db.collection(u201cemployeesu201d);rnQuery query = collection.where(Filter.or(rn Filter.equalTo(u201clocationu201d, u201cGermanyu201d),rn Filter.equalTo(u201ctitleu201d, u201cCEOu201d),rn Filter.greaterThanOrEqualTo(u201ctenureu201d, 5)rn));’), (u’language’, u”), (u’caption’, <wagtail.wagtailcore.rich_text.RichText object at 0x3e5824906790>)])]
Now, say you want to query for performance reviews for a given employee that occurred within the last two years, or where the given rating was higher than 4.
You can nest multiple ORs to accomplish such queries as follows :
- code_block
- [StructValue([(u’code’, u’CollectionReference collection = db.collection(“reviews”);rnQuery query = collection.where(Filter.and(rn Filter.equalTo(“employeeId”, 1234),rn Filter.or(rn Filter.or(rn Filter.equalTo(“year”, 2022),rn Filter.equalTo(“year”, 2021)),rn Filter.greaterThanOrEqualTo(“rating”, 4)rn )rn));’), (u’language’, u”), (u’caption’, <wagtail.wagtailcore.rich_text.RichText object at 0x3e58249069d0>)])]
Nested ORs can be useful when you need to check multiple conditions, but they can also be difficult to read and understand. It is important to use them sparingly and to only use them when necessary.
OR operator can also be used on all our existing query constructs like querying across Collection Groups, or in combination with existing operators like COUNT().
Let’s take an example of Collection Groups called Landmarks, you can now use OR operator to filter collections within the group
- code_block
- [StructValue([(u’code’, u’Query query = db.collectionGroup(“landmarks”).where(Filter.or(rn Filter.equalTo(“type”, “museum”),rn Filter.equalTo(u201cnameu201d, u201cLincoln Memorialu201d))rn);’), (u’language’, u”), (u’caption’, <wagtail.wagtailcore.rich_text.RichText object at 0x3e5824906210>)])]
We know how valuable COUNT() is to you, now you have the ability to combine it with OR to query the data more easily as shown below :
- code_block
- [StructValue([(u’code’, u’Query query = db.collection(“cities”).where(Filter.or(rn Filter.equalTo(“capital”, true),rn Filter.greaterThanOrEqualTo(“population”, 1000000)rn));rnAggregateQuery countQuery = query.count();rnApiFuture<AggregateQuerySnapshot> future = countQuery.get();rnAggregateQuerySnapshot snapshot = future.get();rnlong count = snapshot.getCount();’), (u’language’, u”), (u’caption’, <wagtail.wagtailcore.rich_text.RichText object at 0x3e5824906190>)])]
Extended limits for IN and array contains any
Excitement continues, use multiple IN operators and also combine up to 30 clauses using an IN or array-contains-any operator in Firestore.
Example :
- code_block
- [StructValue([(u’code’, u’Query query = db.collection(u201cpaintsu201d).whereIn(u201ccoloru201d,rn Arrays.asList(“red”, “green”, “blue”, “yellow”, “purple”, “white”,rn “black”, “teal”, “turquoise”, “maroon”, “brown”, “azure”,rn “orange”, “cyan”, “pink”, “khaki”, “gray”, “gold”,rn “silver”, “bronze”, “ivory”, “copper”, “aquamarine”, “amber”,rn “charcoal”, “olive”, “ebony”, “coral”, “lavender”, “magenta”)’), (u’language’, u”), (u’caption’, <wagtail.wagtailcore.rich_text.RichText object at 0x3e5824906a90>)])]
Note : Not_IN operator cannot be combined with the OR operator or have more than 10 clauses.
Next Steps
Enjoy building apps more quickly and conveniently with Firestore.
Please refer to official documentation for more detailed information.