Back

No Limit - List All Documents

  • 0
  • Databases
Infinil
24 Apr, 2023, 15:57

I want to list all documents since 100 limit is removed I believe but without hardcoding the value- .. like i am making a leaderboard so i need to find user's rank after listing all the documents ;-;

TL;DR
The user wants to list all documents for a leaderboard without a hard-coded limit. They are currently using cursor pagination to fetch 100 documents at a time and fetching more if the user is not found. However, they want to find the user's rank after listing all the documents. The suggested solution is to create a new collection for the top 1K users, each row containing user_id and total_highscore. Use a summary function to update this collection periodically or after each insert event in the scores table. The summary function will utilize in-ram variables to hold the top scores collection and check if a new score should be added or if
Infinil
24 Apr, 2023, 15:58

my current approach is to fetch 100 documents and only fetch more if user doesnt exist in those 100 docs (cursor pagination)

Infinil
24 Apr, 2023, 15:59

keep doing it till user is found/no user

Binyamin
24 Apr, 2023, 15:59

But you'll still going to face a limit of the 5,000 totally

Binyamin
24 Apr, 2023, 15:59

Oh, yes that would be the right way

Infinil
24 Apr, 2023, 16:00

oh but the code kinda started to getting complicated so thought i maybe need smth else

Infinil
24 Apr, 2023, 16:00

perhaps thats the best way

Binyamin
24 Apr, 2023, 16:04

Can you elaborate more and the use-case

Infinil
24 Apr, 2023, 16:50

So like I wanna fetch top 10 users in leaderboard

Infinil
24 Apr, 2023, 16:50

With that the rank of current user viewing the leaderboard

Binyamin
24 Apr, 2023, 16:59

Okay, can you show me a simple row?

Infinil
24 Apr, 2023, 19:05

as in single document data?

Binyamin
24 Apr, 2023, 19:05

Yes,

Infinil
24 Apr, 2023, 19:08
Infinil
24 Apr, 2023, 19:08

here are multiple rows since its only 2 columns

Infinil
24 Apr, 2023, 19:09

i basically sort them in descending order

Infinil
24 Apr, 2023, 19:09

for highscore

Binyamin
24 Apr, 2023, 20:27

Got you.

With the available Appwrite query function there it seems there no other way then you suggested.

Meaning as we can't get sum and group-by in the queries then the best way would be to use cursor pagination.

Though, It may be good to create another collection and within it adding totals for top 1K users.

So, Something like this.

Top scores collection A new collection with top 1K users. each row will contain user_id, total_highscroe

The reason there is need for top 1K is for quick access in the Summary function.

Summary function Here we can choose one of these approaches

In case the scores collection get a lot of inserts command, Will run the Summary function hourly.

In case there fair amount of numbers of inserts we can let the function run after any insert event into the scores tables. ( because we're using in-ram variable then it will take less then second most of the time )

The function will utilize an in-ram variable that will hold all the TopScores collection , is size should be about 30KB in the RAM.

Then any time a new score is being added the function will first check to see if there is any matching user id in the in-ram variable, if so it will just add-it and resort the array.

If the new user id is not in the variable then it will be added to a second variable, that will count the value for the IDs that are not in the top 1K.

Then this is the logic when there is a need to recreate the first list.

If any new listing in second variable value + the last value of the the top is more then one of the top 10 then it's time to refetch the list from the database.

TypeScript
  if(newScores[current_id] + topScores[topScores.length - 1] > topScores[9])
    await refetch();

Current user For the current user score we will create another function which select only by the current user id, and will use regular pagination. Or, cursor in case it's common for user to have more then 5,000 scores.

Infinil
25 Apr, 2023, 01:19

ohh thanks for the detailed approach :)

Binyamin
25 Apr, 2023, 01:27

<:appwriteupvote:899677724615016528>

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more