Now you can question Firestore utilizing the OR operator. Your software might typically want to check a number of fields with two or extra values and choose the paperwork that match at the very least one of many values. That is an extension to the IN operator which may very well be used to check a number of values with the identical area.
For instance, when you’re looking for details about cats and canines, you would use the OR operator to mix the 2 search phrases right into a single question:
‘cats = black OR canines = brown’
This could return outcomes that embrace details about each black cats and brown canines.
Firestore now helps OR queries through server and consumer SDKs, with streaming and offline compatibility.
Assist for OR operator now allows you to make the most of Firestore for constructing functions which have to filter a number of fields with two or extra values utilizing any of our present question constructs.
Let’s say you’re working an worker administration portal on Firestore and also you need the flexibility to filter on quite a lot of standards like Title, Group, Location, Tenure and many others., now you can simply make the most of OR to have the ability to do it.
- [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 0x3e9bb5875090>)])]
Now, say you need to question for efficiency evaluations for a given worker that occurred inside the final two years, or the place the given score was increased than four.
You possibly can nest a number of ORs to perform such queries as follows :
- [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 0x3e9bb5795a10>)])]
Nested ORs might be helpful when you should test a number of circumstances, however they may also be troublesome to learn and perceive. You will need to use them sparingly and to solely use them when crucial.
OR operator may also be used on all our present question constructs like querying throughout Assortment Teams, or together with present operators like COUNT().
Let’s take an instance of Assortment Teams referred to as Landmarks, now you can use OR operator to filter collections inside the group
- [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 0x3e9bb57843d0>)])]
We all know how invaluable COUNT() is to you, now you’ve the flexibility to mix it with OR to question the info extra simply as proven beneath :
- [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 0x3e9bb5784190>)])]
Prolonged limits for IN and array accommodates any
Pleasure continues, use a number of IN operators and in addition mix as much as 30 clauses utilizing an IN or array-contains-any operator in Firestore.
- [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 0x3e9bb5784bd0>)])]
Notice : Not_IN operator can’t be mixed with the OR operator or have greater than 10 clauses.
Take pleasure in constructing apps extra rapidly and conveniently with Firestore.
Please seek advice from official documentation for extra detailed info.