0

Firebase OR queries are available from version 4.5.0 in the Flutter cloud_firestore package. I'm using cloud_firestore: 4.8.2 but my OR query does not work. Here is my code:

final ref = FirebaseFirestore.instance.collection('users');

final query = await ref.where(
   Filter.or(
      Filter('followerCount', isGreaterThanOrEqualTo: followerCount),
      Filter('followingCount', isGreaterThanOrEqualTo: followingCount),
   ),
).get();

It seems code is right according to this comment on GitHub:

https://github.com/firebase/flutterfire/pull/10678#issuecomment-1490300789

But I am getting this error instead:

FirebaseError: Invalid query. All where filters with an inequality (<, <=, !=, not-in, >, or >=) must be on the same field. But you have inequality filters on 'followerCount' and 'followingCount'

Any thoughts?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Emre
  • 111
  • 2
  • 10
  • 1
    Hi - with the GitHub code example that you've linked to, they are using a combination of an "isEqualTo" (ie. =) and an "isGreaterThanOrEqualTo" (ie. >=). So, the exception message that you're getting, for your particular query, would not apply to that example. I don't specifically know if what you're encountering is an actual Firebase limitation, or a limitation with Flutterfire, though - but that's the difference between your query and the example (that you are thinking is the same) – Craig Jun 27 '23 at 23:43
  • 1
    These are the notes on Firebase query limitations - https://firebase.google.com/docs/firestore/query-data/queries#query_limitations. I think probably you just need to arrange your filter array slightly differently in the code so that both of the "or" conditions can be applied. Sorry - I've not actually looked at the Flutterfire package before, so I'm not exactly sure how that will be achieved. Hopefully someone else who's done the same thing already will be able to provide an answer – Craig Jun 27 '23 at 23:48
  • Thanks for the answer. If I can use isGreaterThanOrEqualTo and isEqualTo together it's working with a query index. But I can't see any reason to not use isGreaterThanOrEqualTo with 2 different fields. I can't find any document that says using two isGreaterThanOrEqualTo queries is not possible. Maybe this is a flutter-fire issue as you say. Thanks again – Emre Jun 27 '23 at 23:54
  • 1
    This is an older question, but appears to be related to the same issue, and the answer notes the same Firebase restriction on different "or" conditions - the solution appears to be running the two queries separately and the combining the query results https://stackoverflow.com/questions/51190863/filtering-firebase-by-multiple-fields – Craig Jun 27 '23 at 23:55
  • 1
    Actually, on the "Query Limitations" link that I commented above, the second bullet point specifically addresses the same error response that you're getting with your query – Craig Jun 27 '23 at 23:56
  • Sorry for the misunderstanding. Then I understand that it is not possible to use two isGreaterThanOrEqualTo together. I need to resolve it on the client. – Emre Jun 28 '23 at 00:03

1 Answers1

1

The problem is not related to using an OR condition, but the fact that you're trying to perform a range filter on two different fields. As the error message says, all so-called inequality filters must be on the same field.

From the documentation on query limitations:

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

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks, Frank. I'm marking this as the correct answer. In conclusion; Using 'isGreaterThanOrEqualTo' on two different fields will not work in Firestore. – Emre Jun 28 '23 at 16:54