2

I am trying to filter data in firestore and display it in my application. I can see "where Equal To" condition in firestore api but I need "where not Equal to".

How can I do that?

Amsakanna
  • 12,254
  • 8
  • 46
  • 58

1 Answers1

2

You can see from the docs that Where queries do not have a not equal to operator, so this is not possible

The where() method takes three parameters: a field to filter on, a comparison operation, and a value. The comparison can be <, <=, ==, >, >=, or array-contains. For iOS, Android, and Java, the comparison operator is explicitly named in the method.

I would suggest trying to structure your data in a way that there is an alternate field that stores some sort of boolean value for use in your query. Alternatively you could just get the full collection and filter out after. Assuming we are talking about a single doc this shouldn't be much more expensive as an operation

chrismclarke
  • 1,995
  • 10
  • 16
  • 2
    Actually, it is possible. You can use < and > operators to get everything 'around' the item you don't want. So for example; suppose you store the alphabet A, B, C, D, E etc and you want all the letters that are NOT 'N'. This is done by querying all letters less than N and all letters that are greater than N. A real solution though would depend on what the structure is and what is being queried. – Jay Aug 31 '19 at 13:13
  • valid point, although I'd be surprised if this worked out to be very efficient - but again as you say that would depend entirely on the structure being queried. – chrismclarke Aug 31 '19 at 15:26
  • @Jay - Since Firestore doesn't support `or` in queries, that would be 2 separate queries that you need to merge clientside. – charles-allen Sep 01 '19 at 09:11
  • @AjahnCharles Correct, as the links suggest. – Jay Sep 01 '19 at 13:24
  • is there a data structure that would remove the need of the not equal query but provide the same functionality? My current structure is as follows, a "jobs" collection where each document has a field called "createdBy" that contains the uid of the user who created that document, how can i list all the job documents that aren't created by my currently logged in user without combining queries? what changes could i make to my data structure? – Sabeeh Ul Haq Mar 28 '20 at 06:38
  • As I'm guessing there are a lot more jobs that aren't than that are and sounds like every user can access the full list, so I'd say just query the full list of jobs and filter out client-side. I don't think you'd save much with complex queries in this case. – chrismclarke Mar 29 '20 at 18:37