4

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.

1
  • Basically, Cloud Firestore does not allow you do that level of filtering at present. A workaround may be to run the first .whereField query, and then once you have your documents, filter your array for the second query. Commented May 1, 2018 at 18:57

2 Answers 2

5

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 }    
    }
Sign up to request clarification or add additional context in comments.

3 Comments

hi, thanks for helping me, i have parse my data into an array. now, what should i do with constant filteredTeams
filteredTeams should contain the array of teams that you were trying to originally query for. Those with a min > filterMin & max < filterMax.
Firestore limitations on querying are very annoying. This makes Google Firestore unusable for a large project
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.