Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query comparisons with null field

My Firestore collection has documents with a string field, which can be null.

I was expecting that if I query:

Collection("products").Where("producedDate", "<", "2018-01-15")

I would get all products whose "producedDate" is earlier than "2018-10-15", including those whose "producedDate" is null.

But actually I am not getting the nulls.

Is this intended or it's a bug?

like image 245
Daniele B Avatar asked Apr 24 '26 04:04

Daniele B


1 Answers

It was intended to work that way. The documentation states that:

When a query involves a field with values of mixed types, Cloud Firestore uses a deterministic ordering based on the internal representations. The following list shows the order:

  1. Null values
  2. Boolean values
  3. Integer and floating-point values, sorted in numerical order
  4. Date values
  5. Text string values
  6. [...]

Note that it only follows this order when you're running a query with values of mixed types. In your query you're passing a Date value, which means it will only query on values of Date type and not the others (like null for example).

In order to get the null values, you can create a compound query, by adding a second Where:

Collection("products").Where("producedDate", "<", "2018-01-15").Where("producedDate", "==", null)
like image 96
Rosário Pereira Fernandes Avatar answered Apr 25 '26 19:04

Rosário Pereira Fernandes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!