
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
- Relationships restricted to a max depth ...
When I do query like: ``` await _databases.listDocuments( databaseId: AppwriteConfig.DATABASE_ID, collectionId: AppwriteConfig.SERVICES_COLLECTI...
- How can we add more than 8 attributes in...
Hey, when I tried to add attribute in my collection it showed me this Error "The maximum number or size of attributes for this collection has been reached." How...
- implement caching
It it possible to cache response for few minutes? I dont want to create function or implement a whole reverse server just to cache it in somewhere ?
