Skip to content
Init is coming / May 19 - 23
Back

Is there a way of fetching random documents from a collection?

  • 0
  • Databases
psygo
8 May, 2025, 06:07

In SQL, we have the RAND() function, is there an equivalent in Appwrite?

In my app, the user answers questions, which all have an Elo rating. I'm actually unfamiliar with how to choose which question next (matchmaking algorithms), but one way would be to choose a random value, I guess. If any of you guys have a better suggestion on how to tackle my specific problem, please let me know. Ideally, items would not repeat too much, and the range of question Elo should be similar to the user's.

TL;DR
There is currently no direct way to fetch random documents from a collection in Appwrite. To achieve this, developers can either know the total number of documents and use a random offset in the query, or fetch all the documents, determine the count, and then select a random one. There is a feature request for this on GitHub that developers can express interest in: https://github.com/appwrite/appwrite/issues/5205. For the matchmaking algorithm in your app where users answer questions with Elo ratings, selecting random questions is one approach. To avoid repetitions and match the question Elo with the user's level, consider exploring other algorithms or techniques for
Axistro
8 May, 2025, 06:19

Currently Nope. Either you would have to know the total ammount of docuements in your collection then use a random function to genarate some random offset then query using those random offset. Or you can fetch much docs See how much docs are there Then pick a random one. Btw There is a feature request of this issue https://github.com/appwrite/appwrite/issues/5205 You could show intrest in this 🤷‍♂️

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