0

I am trying to implement a conditional clause in a firestore query.


This is my code.

 FirebaseFirestore db= FirebaseFirestore.getInstance();
 db.collection("collection")
      .whereNotEqualTo("field1",value1)
      .whereGreaterThan("field2",start_value)
      .whereLessThan("field2",end_value)
 .get() ....//skip

I am getting the following error..

java.lang.IllegalArgumentException: All where filters with an inequality (notEqualTo, notIn, lessThan, lessThanOrEqualTo, greaterThan, or greaterThanOrEqualTo) must be on the same field. But you have filters on 'field1' and 'field2'


How can I query multiple conditions?

0

1 Answer 1

0

The error message is telling you that Firestore doesn't support multiple inequality filters in a single query on different fields. This is stated in the documentation:

In a compound query, range (<, <=, >, >=) and not equals (!=) comparisons must all filter on the same field.

You have different range filters on both field1 and field2. That simply isn't supported. If you want to know the results of this type of query, you're going to have to perform two different queries, one for each field with the range you want for that field, and merge the results in your app by comparing each of the results with your criteria. Or you will have to come up with a new way of modeling your data that does support the query you want.

Sign up to request clarification or add additional context in comments.

5 Comments

However, if you go to the site below, it says that complex queries are possible. firebase.google.com/docs/firestore/query-data/… In the compound queries part For example citiesRef.whereEqualTo("state", "CA") .whereGreaterThan("population", 1000000); This is made possible.
That's different - study that query carefully. It only has one range filter, and one inequaltiy filter. You just can't have more than one range filter.
Thank you. If it's one range Shouldn't this be possible? db.collection("collection") .whereNotEqualTo("field1", value1) .whereGreaterThan("field2",start_year);
No, that's actually two inequality filters, which is also not allowed. Not equals is actually itself two range filters on a single field.
'not equal' works as a range filter condition. I only understood it now. Thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.