How to query Firestore for game highscores & date at same time?

Viewed 77

I'm new to firebase. I made a game similar to flappy bird, and now I would like to add a leader board.

Could someone help me organize the game score data in firestore so that I can run queries to retrieve:

Top 12 high scores achieved in the last 7 days

Or

Lowest high score recorded in the last 24hrs?

Since these queries contain filters on different fields (score & date) I'm not sure how to do this in firestore.

My initial attempt was to create a collection containing documents, and each document holds playerName, score, date. But it seems I can't query the data if it is formatted this way.

For example If I have 3 documents containing this data:

{ playerName: 'A', score: 500, date: 1649079891803 },
{ playerName: 'B', score: 400, date: 1649079891802 },
{ playerName: 'C', score: 567, date: 1649079891801 },

and I run this query:

query( collection.ref,
  orderBy( 'date', 'desc' ),
  where( 'date', '>=', last24hrs ),
  orderBy( 'score' ),
  limit( 1 ),
);

firebase is returning to me playerName A, score 500 rather than Player B score 400 which is the lowest one.

If I make player C's date be the largest in the group of 3, then firebase will return it ...

I'm looking for a query that will return the lowest score in the last day

0 Answers
Related