Home > OS >  Query if field equals value or is null
Query if field equals value or is null

Time:08-06

I am trying to filter a collection by a field value and also get all entries where the field is equal to null. Since the in operator does not support null values, I can not take the simple route and query for:

where('field', 'in', ['value', null])

I am currently solving this issue by having two queries. One getting the entries where the field is null and one getting the entries where the field equals the value:

where('field', '==', 'value')
where('field', '==', null)

But this returns me more entries than necessary, and I have to sort and merge the entries client side. I would rather have a single query return me all entries, so I can do things like pagination across the combined list, rather than paginating over both list separately.

Is there any method to accomplish this?

CodePudding user response:

No, there is currently no alternative to making multiple queries for each condition. Firestore does not support logical "OR" queries, except for "in" (which does not work for this specific case, as you have seen).

CodePudding user response:

In addition to Doug's answer, I'll say that it might be a workaround, which is instead of storing null, you can store null as a string. That being said, the following query:

where('field', 'in', ['value', 'null'])
//                                          
  • Related