Reputation: 1603
The Firestore documentation says:
Queries with a
!=
clause. In this case, you should split the query into a greater-than query and a less-than query. For example, although the query clausewhere("age", "!=", "30")
is not supported, you can get the same result set by combining two queries, one with the clausewhere("age", "<", "30")
and one with the clausewhere("age", ">", 30)
.
I need to grab users that are in certain age range so this seems like a great way for grabbing relevant ones but unfortunately, I apparently don't understand how to use this.
let potentialMatchesRef = db.collection("users")
.whereField("isBanned", isEqualTo: false)
.whereField("isHidden", isEqualTo: false)
.whereField("location.country", isEqualTo: user.location.country)
potentialMatchesRef
.whereField("age", isGreaterThanOrEqualTo: user.preferences.ageRange.from)
.whereField("age", isLessThanOrEqualTo: user.preferences.ageRange.to)
This doesn't work and when I use where("age", ">", 30)
I get error Value of type 'Query' has no member 'where'
.
UPDATE: Apparently they don't have dedicated docs for Swift.
If I use this
let potentialMatchesRef = db.collection("users")
.whereField("isBanned", isEqualTo: false)
.whereField("isHidden", isEqualTo: false)
.whereField("location.country", isEqualTo: user.location.country)
.whereField("age", isLessThanOrEqualTo: to)
.whereField("age", isGreaterThanOrEqualTo: from)
I get error Error getting documents: Error Domain=FIRFirestoreErrorDomain Code=9 "The query requires an index.
There are minimum 2 fields and I can't write age in both. Please, can somebody help.
Upvotes: 1
Views: 7258
Reputation: 317372
When the documentation says you can "combine two queries", that means you should be making two completely different queries, executing them separately, and combining the results on the client to come up with the final set of documents.
What you're trying to do is make a single query with two clauses that filter on age. This won't work, because all the conditions in a single query are logically AND'd together (there are currently no logical OR queries in Firestore). Think about it this way - the set of documents where "age is > 30 AND age < 30" is always going to yield 0 documents because any value for age can't satisfy both of those requirements.
Upvotes: 4