Reputation: 149
I'm running a basic query to a small DB that is structured as follow:
Let's say first 2 fields have specific information:
But for the third field, I can query all values with the specific color: red, blue etc. The question I have is how can I query the third field using:
If "All Colors" is not a specific value? I've used: .whereField(CAR_COLOR, isGreaterThanOrEqualTo or arrayContains or arrayContainsAny, etc) and it doesn't work. I get no results. Any ideas? Thank you!
Upvotes: 0
Views: 88
Reputation: 52575
Based on the comments discussion, it sounds like you want to be able to include whereField
in your query chain, but not actually execute it if certain conditions are true/false.
I suggest adding an extension to Query
:
extension Query {
func whereField(useCondition: Bool, _ field: String, arrayContains: String) -> Query {
if useCondition {
return self.whereField(field, arrayContains: arrayContains)
} else {
return self
}
}
}
Then, you could do something like this:
db.collection("car").whereField(useCondition: !allColors, "carColor", arrayContains: searchedColor)
Note that you may have to adjust this to fit the variety of whereField
that you need.
Upvotes: 2