newDeveloper
newDeveloper

Reputation: 35

kotlin whereNotEqualTo doesn't work for a field that has null value in firestore

enter image description here enter image description here enter image description here enter image description here BJ document has a null value of the capital field which should have not taken by  whereNotEqualTo

BJ document has a null value of the capital field which should have not taken by whereNotEqualTo

 val citiesRef = db.collection("cities").whereNotEqualTo("capital", true)
    try {
        val documents = citiesRef.get().await()
        for (document in documents){
            Log.d(TAG,"${document.data}")
        }
    }catch (e:Throwable){
        Log.d(TAG,e.message.toString())
    }

This code is not working despite the documentation saying that

Note that null field values do not match != clauses, because x != null evaluates to undefined.

but after I altered the null value with true or false doesn't matter which one is chosen, the code works well. can somebody explain to me what is the problem I encountered?

Upvotes: 1

Views: 352

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138969

When you are using the following query:

val citiesRef = db.collection("cities").whereNotEqualTo("capital", true)

It means that you are trying to get all documents in the cities collection where the capital field holds the opposite value of the Boolean true, which is false. So you'll only get the documents where the capital field holds the value of false. The above query is the same with:

val citiesRef = db.collection("cities").whereEqualTo("capital", false)
                                           👆

Since null, is not the negation of true, the documents that have the capital field set to null, won't be returned. And it makes sense since null is not a boolean type, not a string type, nor any other supported data types. Null represents "none" or "undefined" and it's a distinct data type and cannot be considered "not equal" to any other data type.

That's the same as the docs states:

Note that null field values do not match != clauses

Upvotes: 1

Related Questions