According to this documentation of compound queires I can make queries like this:
query(
collection(db, 'users', user.value.id, 'leaderboard'),
and(
where('series.seriesStartDate', '<=', new Date()),
where('score', '>', 0),
),
limit(5)
)
I have already made the index:
and tested the query that it is working on a dashboard
But when I make the query in my application it is not working and I am getting the error
FirebaseError: Invalid query. All where filters with an inequality (<, <=, !=, not-in, >, or >=) must be on the same field. But you have inequality filters on ‘series.seriesStartDate’ and ‘score’
What am I doing wrong?
2
Answers
Your screenshot shows that you created an index for one specific leaderboard subcollection nested under a users document ID that starts with "CAU". That index is not going to work for queries on any other collection or subcollection, only the leaderboard subcollection for that one user.
If you want to perform this query, you’re going to have to:
Either that, or your’re going to have to move all of the documents from all of the leaderboard subcollections to a new top-level collection, also providing the user IDs as a field for filtering.
The Firebase Console returns the desired results because most likely it uses the Web Version 9 "modular" syntax. Your code, however, uses the Web Version 8, also known as the "namespaced" syntax.
By the time I write this answer, there is no way you can query with range and inequality filters on multiple fields using Web Version 8, but only using Web Version 9. Besides that, I did not find any updates in the Release Notes that say that Firebase enabled queries with range and inequality filters on multiple fields. On the other hand, starting with Cloud Firestore version 25.0.0 for Android:
So to solve this, I recommend you change the syntax to Web Version 9 "modular", to avoid that error.