Back

[SOLVED] How To get random document from collection

  • 0
  • Databases
  • Flutter
Binyamin
10 Mar, 2023, 02:26

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

TL;DR
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
10 Mar, 2023, 02:31

This sounds like a reasonable approach to me

Binyamin
10 Mar, 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
10 Mar, 2023, 02:34

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

Binyamin
10 Mar, 2023, 02:35
Drake
10 Mar, 2023, 02:36

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
10 Mar, 2023, 02:36

A GitHub issue would be better

Binyamin
10 Mar, 2023, 02:37

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

Binyamin
10 Mar, 2023, 02:37

Great Thanks @Steven

Binyamin
10 Mar, 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
10 Mar, 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
10 Mar, 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
10 Mar, 2023, 03:43

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

Drake
10 Mar, 2023, 03:43

The maximum per page right now is 100

Drake
10 Mar, 2023, 03:44

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

Binyamin
10 Mar, 2023, 03:44

100 or 5000 I'm confused

Drake
10 Mar, 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
10 Mar, 2023, 03:45

Oh so the number will not mix, good

Drake
10 Mar, 2023, 03:45

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

Drake
10 Mar, 2023, 03:46

So actually...you wouldnt even be able to offset more than 5k πŸ˜…

Binyamin
10 Mar, 2023, 03:46

πŸ˜‚πŸ₯Ή

Binyamin
10 Mar, 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
10 Mar, 2023, 03:48

Thanks for everything

Drake
10 Mar, 2023, 19:12

[SOLVED] How To get random document from collection

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more