
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
- Can't reach Frankfurt server
I have been developing an IoT device that senses an event, takes a picture and pushes the jpg to an appwrite project. I'm using a Sixfab Pico LTE board that cou...
- Flutter - FCM: App Crashes when receivin...
- Firebase Messaging Causes ANR on Notific...
I am working on a Flutter project where I have set up notifications using Firebase Messaging. Everything was working perfectly until recently. After coming back...
