I'm running a basic query to a small DB that is structured as follow:
- carMake:
- carModel:
- carColor:
Let's say first 2 fields have specific information:
- carMake: "Ford"
- carModel: "Mustang"
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:
- carColor: "All Colors"
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!
CodePudding user response:
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.