Firestore Queries - Order & Filter

Hi all -
I’m trying to create a leaderboard that will pop up after each level with the user’s score as well as the score of other users after that same level:

after level 1

  1. User1 - 10
  2. User2 - 11
  3. User3 - 12

after level 2

  1. User1 - 20
  2. User2 - 22
  3. User3 - 24

I have been working with the example leaderboard made with Firestore queries - which has been super useful, however, when I try to add an additional filter to the query, the query breaks.

I have used the example data structure, slightly modified:

{
name: “User1”,
score: 10,
level: 1
}

I was hoping it would be as easy as adding a filter to the query to remove all documents with scores after levels other than level 1:

Issue is that when I have both “order” & “filter” the leaderboard breaks.

If I remove “order” and “limit”, “filter” works. This isnt useful as the leaderboard is no longer a leaderboard, just a list of names and scores unorganized.

If I remove “filter”, “order” & “limit” work. This isnt useful because I get scores for all levels (e.g. score for user1 who has completed all levels will be presented after level 1, which isn’t comparable)

Any thoughts would be appreciated as I need both of these to work. Not sure why it’s not working the way I have it (probably my fault i’m pretty new at this). Thanks!

I wrote this code

I hope it help you

Thanks jumpingj - I looked through your thread, definitely helpful but it looks like your example overwrites scores posted to avoid duplicate scores, no? I need to store multiple scores for one user, and filter out irrelevant results.

Example:
User1 plays on Jan. 1, 2021 and posts following scores:
Level1 - 10
Level2 - 20
Level3 - 30
Final Score - 30

User2 plays on Jan. 2, 2021 and posts following scores:
Level1 - 11
Level2 - 22
Level3 - 33
Final Score - 33

Lets say User3 plays on Jan 3, 2021 and scores 12 on level 1. After completion of level 1, I want the leaderboard to appear as follows:

Leaderboard:

  1. User3 - 12 (level 1 score)
  2. User2 - 11 (level 1 score)
  3. User1 - 10 (level 1 score)

Currently, without the ability to filter results, the scoreboard will show all stored scores:

Leaderboard after completion of level1:

  1. User2 - 33 (level 3 score)
  2. User1 - 30 (level 3 score)
  3. User2 - 22 (level 2 score)
  4. User1 - 20 (level 2 score)
  5. User3 - 12 (level 1 score)
  6. User2 - 11 (level 1 score)
  7. User1 - 10 (level 1 score)

When the leaderboard appears after level 1, I only want level 1 scores to appear, which is why I added the line
Filter query “GetScores” to remove documents whose field “level” is not "=="1
However, when I add in that line, the leaderboard doesn’t load.

Any thoughts out there or any hints on where I might be going wrong would be appreciated. thanks all.

I suppose you have two possibilities

1 send json and not single value to firebase. In json you can store multiple scores

2 Use add and not update to insert the scores and add the structure with date and so on