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
Summary
The user wanted to know how to get a random document from a collection. They mentioned in the thread that the maximum limit is 100 documents per request, and the maximum total and offset is 5000. There was a suggestion to use cursor-based pagination for collections with more than 5000 documents. Another suggestion was to use a cron function that runs every N minutes to update the collection length in another collection flag. The thread ended with a suggestion to submit a feature request for a built-in method to get a random document.
Drake
Admin
Mar 10, 2023, 02:31
This sounds like a reasonable approach to me
Binyamin
Rank 7: Quantum Computer
Mar 10, 2023, 02:32
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?
Drake
Admin
Mar 10, 2023, 02:34
Btw, it might be good to submit a feature request for a built in method to get a random document
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
Drake
Admin
Mar 10, 2023, 02:36
A GitHub issue would be better
Binyamin
Rank 7: Quantum Computer
Mar 10, 2023, 02:37
Ohh nice!! then the function would run automatically anytime that the table (collection) size changed
Binyamin
Rank 7: Quantum Computer
Mar 10, 2023, 02:37
Great
Thanks @Steven
Binyamin
Rank 7: Quantum Computer
Mar 10, 2023, 03:38
Also @Steven now as I think about it. How I actually get the number of rows without accessing the MariaDB it self?
Drake
Admin
Mar 10, 2023, 03:39
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
Binyamin
Rank 7: Quantum Computer
Mar 10, 2023, 03:41
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?
Binyamin
Rank 7: Quantum Computer
Mar 10, 2023, 03:43
The only way I'm thinking of its to keep iterate 5K till the end
Drake
Admin
Mar 10, 2023, 03:43
The maximum per page right now is 100
Drake
Admin
Mar 10, 2023, 03:44
Honestly, if you're self hosting right now, I would just do the increment/decrement on create/delete
Binyamin
Rank 7: Quantum Computer
Mar 10, 2023, 03:44
100 or 5000
I'm confused
Drake
Admin
Mar 10, 2023, 03:45
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
Binyamin
Rank 7: Quantum Computer
Mar 10, 2023, 03:45
Oh so the number will not mix, good
Drake
Admin
Mar 10, 2023, 03:45
Maximum limit is 100 so you can only fetch 100 documents per request. Maximum total and offset is 5000
Drake
Admin
Mar 10, 2023, 03:46
So actually...you wouldnt even be able to offset more than 5k 😅
Binyamin
Rank 7: Quantum Computer
Mar 10, 2023, 03:46
😂🥹
Binyamin
Rank 7: Quantum Computer
Mar 10, 2023, 03:48
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
Binyamin
Rank 7: Quantum Computer
Mar 10, 2023, 03:48
Thanks for everything
Drake
Admin
Mar 10, 2023, 19:12
[SOLVED] How To get random document from collection