1

I have a collection that includes documents for each user for my app and these documents include the points for each user. I want to get the order of each user. I know that I can get all the documents in the collection, sort them by their points and get the order of the current user. But this approach will require reading every document (if there are 10,000 users, that means 10,000 reads for every time a user wants to see his/her rank) and will increase my bill unnecessarily. I also tried to use cloud functions, but the function using the same logic will also consume read quota as above I guess. Is there a solution that I can get the order of a single user without reading the whole collection? Thanks.

uugurbas
  • 11
  • 2
  • Do you mean you want to get the position of a user like in a ranking board? – Alb Jan 16 '22 at 15:50
  • You need to read about pagination https://firebase.google.com/docs/firestore/query-data/query-cursors – Mises Jan 16 '22 at 15:53
  • To understand better, do you need to get the position of a user in a 10k users collection based on a field? – Alex Mamo Jan 17 '22 at 14:59
  • Yes, @Albert and Alex Mamo. Users earn points, and I want to show their position based on their points on a leaderboard page. The one with the highest point will be the first, and the one with the lowest points will be the 10.000th user. It will be a typical leaderboard that will show ten users with the highest points (this is the easy part), and the current user with his/ her rank among the 10k users. Shortly it will show eleven users, the first ten users plus the current user to limit the reading count to eleven. Something like this https://ibb.co/kXLHc66 – uugurbas Jan 17 '22 at 16:32
  • Does this answer your question? [Leaderboard ranking with Firebase](https://stackoverflow.com/questions/46720997/leaderboard-ranking-with-firebase) – Alb Jan 17 '22 at 16:37
  • Thank you very much – uugurbas Jan 17 '22 at 17:27

0 Answers0