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
- I recently switched to TablesDb. When li...
olddb.list_documents( queries =[ Query.order_desc("timestamp"), Query.equal("isPosted",[False]) ] ) Above works fine and reruns documents But below don't return...
- Database error validating 'min' and 'max...
I keep seeing an error when I try to add a column to a database table within the AppWrite cloud UI. I also see this error when trying to create or update a reco...
- Fine grained permissions for webRTC hand...
Hi, I am building a WebRTC P2P app for a university project and have hit a security limitation regarding permissions for anonymous users. The Architecture: We ...