
I'm trying to find a way to obtain random document from a collection, I didn't find anything about it in the documentation.
The only idea I have so far is to know the length of the Collection and then enter random number into the offset
Query function. But, this seems to manual for me
If any one have an idea how to achieve it will help me
Thanks

This sounds like a reasonable approach to me

So let's say I will do cron function that run every N minutes that will update the Collection length in other collection flag Is that sound a good approach?

Btw, it might be good to submit a feature request for a built in method to get a random document

This is done here? https://github.com/appwrite/appwrite/discussions/categories/ideas

Yes that works. When we add support for atomic increment updates, you can have a function that triggers on create or delete and increment or decrement

A GitHub issue would be better

Ohh nice!! then the function would run automatically anytime that the table (collection) size changed

Great Thanks @Steven

Also @Steven now as I think about it. How I actually get the number of rows without accessing the MariaDB it self?

If you have less than 5k, you can use list documents and grab the total from the response.
If you have more than 5k, you'll have to use cursor based pagination to iterate over all the documents

Mmm I see. In this use case there will be surley more then 5K and actullay the only reason I would need the rows number is for the random function.
So the 5K limit its also trough API side?

The only way I'm thinking of its to keep iterate 5K till the end

The maximum per page right now is 100

Honestly, if you're self hosting right now, I would just do the increment/decrement on create/delete

100 or 5000 I'm confused

On self hosted, because you only have 1 worker, the events are handled serially. On cloud, we've scaled out the workers so they're handled concurrently

Oh so the number will not mix, good

Maximum limit is 100 so you can only fetch 100 documents per request. Maximum total and offset is 5000

So actually...you wouldnt even be able to offset more than 5k 😅

😂🥹

I think my approach for now will be counting manually and I'll create few table each one upto 5000 And the random will be on which table (collection) and the with in it

Thanks for everything

[SOLVED] How To get random document from collection
Recommended threads
- Flutter OAuth2 Google does not return to...
When the flow starts, the browser opens, I select an account, and it keeps showing: """ Page not found The page you're looking for doesn't exist. `general_rout...
- JSON and Object Support in Collection do...
I am working with Next.Js and Appwrite Cloud, I am relatively New to Appwrite but i have noticed there is no direct support of JSON and Object support in attrib...
- list() is very slow; eventually shows no...
When I use the web browser to view the collections in my database, the documents they contain are normally displayed within a few seconds. For a few days now, h...
