I am using Firestore and I have very simple queries with equality filter where I am required to do orderBy (because I have to limit the results). I can't use startAfter withour orderBy, because Firebase throws an error in that case.
FirebaseError: Too many arguments provided to Query.startAfter(). The number of arguments must be less than or equal to the number of Query.orderBy() clauses
However, I can't use orderBy with an equality filter. Example:
query.where('tags.supporter', '==', true).orderBy('tags.supporter');
This throws a Firebase error:
FirebaseError: Order by clause cannot contain a field with an equality filter tags.supporter
Funny enough, this works just fine :-
query.where('tags.supporter', '<=', true).where('tags.supporter', '>=', true)
^ this works and gives me tags.supporter == true, but I can't use it with multiple filters.
Example:
query.where('tags.supporter', '==', true).where('tags.volunteer', '==', true)
^ works, but I can't use startAfter without orderBy. But if I try to use orderBy, I get the error :-
query.where('tags.supporter', '==', true).where('tags.volunteer', '==', true).orderBy(...)
^ doesn't work!
I can't do the <= >= hack because it requires composite index but I can't set that up because these are user-added tags.
Any ideas?
If this truly is a limitation then it should be mentioned explicitly in the documentation because it would have influenced my decision. Now I'll have to either build a lot of logic in my own app to filter results if this truly is a limitation.
After a lot of trial and error, I figured it out.
You can use .orderBy(firebase.firestore.FieldPath.documentId())
. This allows you to use multiple equality filters and still do limit and startAfter.
Suppose you are given a array of 10 elements ,having each of its item the value '5' . Now you have to sort the array (or to say , orderBy ascending ), how will you do so. Answer is its 'already sorted'.
Same goes with your Query having first filter on age and then orderBy on that same property i.e age .
It works fine with range filter age>=7
andage<=7
because it includes the complete range of age and finally contain something to orderBy.
query.where("age", "<=", 7).where("age", ">=", 7).orderBy("age)
Filter with equality ( on age ) returns you a queryset of data having same value of the property on which you applied the filter, i.e each and every data object in your returned queryset is of uniform age ,there is no distinction on the basis of age (yes you can and should be applying orderBy on other properties for sure).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With