简体   繁体   中英

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

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述 BJ 文档的大写字段为空值,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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM