skip to Main Content

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:

Query index

and tested the query that it is working on a dashboard
user 1
user 2

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


  1. 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:

    1. Use a collection group query on the "leaderboard" collectoin group instead of a normal collection query.
    2. Add the user’s ID as a field in each document in each user’s leaderboard subcollection.
    3. Add a filter on your query for the user ID whose subcollection documents you want to consider.
    4. Create the relevant index on the leaderboard collection group (not just the one nested subcollection).

    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.

    Login or Signup to reply.
  2. 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:

    Enabled queries with range and inequality filters on multiple fields.

    So to solve this, I recommend you change the syntax to Web Version 9 "modular", to avoid that error.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search