0

I've read this link Firestore: How to get random documents in a collection but it looks like my problem is different. Because these solutions are not good for queries with where conditions.

My collection looks like this:

enter image description here

I have 140K documents in this collection. I want to get random X amount of documents filtered by any category.

My code looks like this (Swift):

Firestore.firestore().collection("quotes")
    .whereField("categories", arrayContainsAny: ['change'])
    .limit(to: 25)

This code always returns the same documents. How can I get random documents filtered by category?

Tolgay Toklar
  • 4,151
  • 8
  • 43
  • 73
  • 1
    Did you see https://stackoverflow.com/questions/46798981/firestore-how-to-get-random-documents-in-a-collection, and can it be applied to your use-case? – Frank van Puffelen Jan 19 '22 at 19:35
  • No it covers no where condition situations. – Tolgay Toklar Jan 19 '22 at 19:44
  • Did you try adding your condition the queries that are shown in that answer? What didn't work about it when you did that? – Frank van Puffelen Jan 19 '22 at 20:27
  • 1
    Hi @TolgayToklar, you could also use `placeId` as reference to your query. First, you should create a function that generates a random number. You may refer to this [thread](https://stackoverflow.com/questions/24007129/how-does-one-generate-a-random-number-in-swift) for generating random number. Add this `.whereField("placeId", isGreaterThanOrEqualTo: randomNumber)` to your query after getting a random number. This will randomize getting documents from Firestore based on the random number you generated and the placeId of your documents. Let me know if this can be applied to your use-case. – Marc Anthony B Jan 20 '22 at 05:28
  • No it cannot because categories are not equally placed. Let’s say: you created a random number and it is 10. And you need to get a document from “Life” category. But all documents that contains “Life” category’s placeId less than 10. Firestore will return 0 document. – Tolgay Toklar Jan 20 '22 at 05:48

1 Answers1

1

I agree with Frank, the link has everything you need. You can chain the where conditions.

After following the steps, you can query like:

Firestore.firestore().collection("quotes")
    .whereField("categories", arrayContainsAny: ['change'])
    .whereField("random", isGreaterThanOrEqualTo: random)
    .limit(to: 25)

edit ahhh I see your point. I would work around this issue by querying again.

  • if the result is less than what's needed (25) run another query, something like
Firestore.firestore().collection("quotes")
    .whereField("categories", arrayContainsAny: ['change'])
    .whereField("random", isLessThan: random)
    .limit(to: 25)

You can then store some of the results of both queries to get your 25

knicholas
  • 520
  • 4
  • 10
  • No it cannot because categories are not equally placed. Let’s say: you created a random number and it is 10. And you need to get a document from “Life” category. But all documents that contains “Life” category’s placeId less than 10. Firestore will return 0 document – Tolgay Toklar Jan 20 '22 at 05:48
  • @TolgayToklar I see you point, check my edit. Would that work? probably not the best since you will have to query again...? – knicholas Jan 20 '22 at 06:37
  • 1
    Yeah, I guess this would work. I am gonna try and let you know :) – Tolgay Toklar Jan 20 '22 at 07:07