madhatter989
madhatter989

Reputation: 281

Firestore Queries Swift 4 for range

i want to ask a question related to firestore queries swift. I have firestore structure as below:

enter image description here

currently i develop a search system, where user can filter min and max persons per team. below are the queries for .collection("team")

Firestore.firestore().collection("team")
.whereField("min", isGreaterThanOrEqualTo : filterMin)
.whereField("max", isLessThanOrEqualTo : filterMax)

Expected: if user place filterMin = 0 and filterMax = 600 data for team1 and team2 will displayed, if user place filterMin = 3 and filterMax = 500, only data for team2 will displayed.

But the problem is if i using the code above, i got an error as below

'Invalid Query. All where filters with an inequality (lessThan, lessThanOrEqual, greaterThan, or greaterThanOrEqual) must be on the same field. But you have inequality filters on 'min' and 'max''

I have already read the documentation, but it seems does not help me much. I am still new in swift development and firebase. thanks for helping me.

Upvotes: 4

Views: 3654

Answers (2)

rbaldwin
rbaldwin

Reputation: 4858

Cloud Firestore does not at present allow you to do multiple inequality filters. A workaround may be to run the first .whereField query, and then once you have your documents, filter your array for the second query.

    Firestore.firestore().collection("team")
    .whereField("min", isGreaterThanOrEqualTo : filterMin)
        .getDocuments { (snapshot, error) in
            if let snapshot = snapshot {
                for document in snapshot.documents {
                    let data = document.data()

                    //parse your data into an array of teams
                }
            }

            let filteredTeams = teams.filter { $0.max < filterMax }    
    }

Upvotes: 5

Chris
Chris

Reputation: 8020

You cannot have an inequality operator on multiple fields, a field in your being being either min or max. Firestore only supports one inEquality operator (<, >, <=, >=) and then as many eqaulity operators (==) as you want.

You can read more about it under 'Compound Queries' here

Upvotes: 0

Related Questions