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?
CodePudding user response:
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

