so since we cannot do x != null
(I think this resulting in a runtime error) and not-in [...something, null]
match nothing, is there any point using null in Firestore? or we should just avoid it and use the default value.
CodePudding user response:
so since we cannot do x != null
I think this results in a runtime error.
No, it doesn't.
Is there any point using null in Firestore?
Yes, you can query Firestore to get documents that contain a particular field that holds the value of null
. Obviously, the opposite is available.
Edit:
not-in
queries exclude documents where the given field does not exist.
It makes sense since Firestore queries can only return results based on the existence of a field and not on the absence.
A field exists when it's set to any value, including an empty string (
""
),null
, andNaN
(not a number).
Even if we add an empty string, null
, or NaN
, it doesn't mean that the field doesn't exist. It definitely exists and holds one of those values.
Note that x != null evaluates to undefined. A not-in query with null as one of the comparison values does not match any documents.
That's normal since a value from the list is null. You cannot compare null for non-null values.
CodePudding user response:
Note that x != null evaluates to undefined. A not-in query with null as one of the comparison values does not match any documents.
source https://firebase.google.com/docs/firestore/query-data/queries#not-in
there are 2 statements:
x != null evaluates undefined
A not-in query with null as one of the comparison values does not match any documents
I examine the statements one by one by running some tests
- x != null evaluates undefined
this statement is false, it works fine, but probably because the sentences is not complete, it should be x != null evaluates undefined for non-exist field
- A not-in query with null as one of the comparison values does not match any documents
this statement however, is true
where("x", "not-in", [null])
and where("x", "not-in", [...something, null])
will always return empty array even if there is document that fulfil the condition
there is however, a 3rd statement which is missing from the doc
where("x", "not-in", [...something])
will always exclude doc where x
is null
update I discovered 4th behaviour
when null 1 data type, eg null string
x != something
will include doc where x is null
when there is more than 1 data type, eg null string number
x != something
will not include doc where x is null
so yeah, I would avoid null