kotlin whereNotEqualTo 不适用于 firestore 中具有空值的字段
kotlin whereNotEqualTo doesn't work for a field that has null value in firestore
BJ 文件中有一个资本字段的空值,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())
}
尽管文档中说
,但此代码无法正常工作
Note that null field values do not match != clauses, because x != null evaluates to undefined.
但是在我用 true 或 false 更改空值之后,无论选择哪个都无所谓,代码运行良好。有人可以向我解释一下我遇到的问题吗?
当您使用以下查询时:
val citiesRef = db.collection("cities").whereNotEqualTo("capital", true)
这意味着您正在尝试获取 cities
集合中的所有文档,其中 capital
字段包含布尔值 true 的 相反值,这是错误的。因此,您只会获得 capital
字段值为 false 的文档。上面的查询与:
相同
val citiesRef = db.collection("cities").whereEqualTo("capital", false)
因为 null 不是 true 的否定,所以 capital
字段设置为 null 的文档不会被返回。这是有道理的,因为 null 不是布尔类型,不是字符串类型,也不是任何其他 supported data types。 Null 表示“none”或“未定义”,它是一种独特的数据类型,不能被视为“不等于”任何其他数据类型。
这与文档所述相同:
Note that null field values do not match != clauses
BJ 文件中有一个资本字段的空值,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())
}
尽管文档中说
,但此代码无法正常工作Note that null field values do not match != clauses, because x != null evaluates to undefined.
但是在我用 true 或 false 更改空值之后,无论选择哪个都无所谓,代码运行良好。有人可以向我解释一下我遇到的问题吗?
当您使用以下查询时:
val citiesRef = db.collection("cities").whereNotEqualTo("capital", true)
这意味着您正在尝试获取 cities
集合中的所有文档,其中 capital
字段包含布尔值 true 的 相反值,这是错误的。因此,您只会获得 capital
字段值为 false 的文档。上面的查询与:
val citiesRef = db.collection("cities").whereEqualTo("capital", false)
因为 null 不是 true 的否定,所以 capital
字段设置为 null 的文档不会被返回。这是有道理的,因为 null 不是布尔类型,不是字符串类型,也不是任何其他 supported data types。 Null 表示“none”或“未定义”,它是一种独特的数据类型,不能被视为“不等于”任何其他数据类型。
这与文档所述相同:
Note that null field values do not match != clauses