0

We are able to run null inequality just fine on the JavaScript SDK (client or admin), but we are unable to in PHP? To me it seems this is a backend feature which is already supported by Firestore, but throws an error in PHP.

->where('preorders', '!=', null)

Uncaught InvalidArgumentException: Null and NaN are allowed only with operator EQUALS.

Whereas the equivalent in JS SDK works just fine.

query(q, where('preorders', '!=', null))

Since I cannot open an issue in google's PHP SDK repo here, is there any way we can find a reason as to why this is not possible?

Eduard
  • 3,395
  • 8
  • 37
  • 62

1 Answers1

2

The documentation says, :

“A field exists when it's set to any value, including an empty string (" "), null, and NaN (not a number). Note that null field values do not match != clauses, because x != null evaluates to undefined.”

Explanation : Yes, even if we add an empty string, null,( supported data type in Firestore) or NaN, it doesn't mean that the field doesn't exist. It definitely exists and holds one of those values. And coming to when we compare x != null in where clauses, x != null evaluates undefined, but for non-exist fields. And undefined is not a supported data type in Firestore, according to this Firestore supported data type.
So we can compare .where(x!=null) and it is supported, but it evaluates to undefined for non-existent fields.

As mentioned in similar thread, the version release of v7.21.0 in Firestore JS SDK supports the != operator with where clause from version 7.21.0
But while digging deeper in the documentation, I found that php5 supports != operator as you can see in the code snippet, but the php8 does not support != operator yet and the workaround is using not-in instead of it as shown in this code snippet.
Maybe you are trying to use php version >5 and hence the error. There is already an open issue on this, in GitHub. You may follow the link for updates and changes or you can create a new request here.

Roopa M
  • 2,171
  • 4
  • 12
  • What do you mean by `x != null evaluates to undefined in PHP`? Running `var_dump(4 != null)` returns true, not undefined. Also, I don't see any point in the lack of support, since this is processed in Firestore's backend, not clientside. Therefore, I think this is a lack of implementation in PHP on the client side, not allowing null with inequality operator. – Eduard Mar 08 '22 at 20:40
  • I have edited the answer please have a look at it and let me know if you have any doubts. – Roopa M Mar 09 '22 at 09:12
  • I fully understand how "null" works in document fields, and I am not expecting to find documents which don't have that specific field set, in my snapshot. What I was wondering is why the lack of null inequality in PHP, since this is a backend feature (firestore) and not a client side operation. But your link to the snippets explains it. As I guessed, there's no real reason why it is not supported in the latest PHP SDKs aside the lack of implementation on Google's end. – Eduard Mar 09 '22 at 10:19