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?

5
  • 1
    Did you see stackoverflow.com/questions/46798981/…, and can it be applied to your use-case? Commented Jan 19, 2022 at 19:35
  • No it covers no where condition situations. Commented Jan 19, 2022 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? Commented Jan 19, 2022 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 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. Commented Jan 20, 2022 at 5: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. Commented Jan 20, 2022 at 5:48

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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
@TolgayToklar I see you point, check my edit. Would that work? probably not the best since you will have to query again...?
Yeah, I guess this would work. I am gonna try and let you know :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.