Back

Querying a document in a collection and "shuffling"

  • 0
  • Databases
  • Flutter
SylvainJack
24 Nov, 2023, 14:32

I have a collection of around 10000 items. I need to query a document with the following criterion : Give me a document whose reference attribute is not present in this list. This, I can do with "notequal" method. Here is my question : this condition will be true of many documents : how can I do it so that it doesn't always give me the first document, I kinda need it to pick "randomly" among the 10000... not the first one. Is there a way ? Or do I have to retrieve ALL documents and then shuffle it locally ?

TL;DR
The user wants to query a document in a collection based on a condition. The concern is that the query might always return the first document. The user wants a way to pick a document randomly among the collection's 10,000 items. They are unsure if they should retrieve all documents and shuffle them locally or if there is a way to achieve this directly through the query. A solution is suggested to use a random offset in the query to achieve the desired random selection of documents.
Drake
24 Nov, 2023, 14:43

There is no random query at the moment. You might want to 👍 this issue: https://github.com/appwrite/appwrite/issues/5205

That said, you could try using a random offset

SylvainJack
24 Nov, 2023, 14:51

What is a random offset ?

SylvainJack
24 Nov, 2023, 14:57

I added comment on the issue link you gave me

Drake
24 Nov, 2023, 15:02

Create a random number and then pass that in an offset query

Drake
24 Nov, 2023, 15:03

Did you 👍 the issue?

SylvainJack
24 Nov, 2023, 15:08

yes I just did. So If I need a document in a collection respecting a certain condition : example : any item whose reference isn"'t equal to "CEA1" : my query will download ALL the documents, of just the first one ? How can I tell Appwrite to give me the 10th document respecting the condition ?

Drake
24 Nov, 2023, 15:10

Have you tried looking into offset queries?

SylvainJack
24 Nov, 2023, 15:10

No I didn't know what it was, or that it existed 😉

Drake
24 Nov, 2023, 15:10

Try searching the docs

SylvainJack
24 Nov, 2023, 15:11

It gives answers by "skipping" some documents ?

SylvainJack
24 Nov, 2023, 15:11

I see one line in the docs for that

SylvainJack
24 Nov, 2023, 15:13

OK I see... but for that I would need to know how many documents respect my condition. Is there a way to do this ?

Drake
24 Nov, 2023, 15:15

You can query without the offset and include a limit. Then check the total in the response

SylvainJack
24 Nov, 2023, 15:16

But I fear if I do that, it will keep picking items in the first part of the collection...

Drake
24 Nov, 2023, 15:18

How and why?

SylvainJack
24 Nov, 2023, 15:22

Hmm : My bank has 10000 questions used to create a language test. A question is based on a document (a text, an audio...). In the question item : there is a reference that indicates the reference of the document. In the bank : there are many questions based on a same document. But for a test, a document can only be used once. So when student takes the test : it picks a first question. This means for the following questions the document of the first one can no longer be used. So it means : the next question should be picked in the bank with document not equal to.... . A test has 15 questions total. So I fear that every time the student starts a new test : it's always made up of questions picked in the beginning of the list. (Never Item n°8000 for example, because there will always be items possible before).

Drake
24 Nov, 2023, 15:30

You're going to be filtering with queries so what's the problem?

SylvainJack
24 Nov, 2023, 15:39

I guess I must not understand how it works then. Sorry

Drake
24 Nov, 2023, 15:44

Read the docs and try it out. That would be the best way to figure it out. Feel free to ask follow up questions if you have any

SylvainJack
24 Nov, 2023, 15:46

If I query a document with this condition : one attribute not equal "CEA1". Then what does it do exactly ? It scans the collection until it finds a document respecting the condition ? is this correct ?

SylvainJack
24 Nov, 2023, 15:46

and then downloads the document, correct ?

Drake
24 Nov, 2023, 15:54

Try it

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