所以我正在尝试查找具有字段集且不为空的所有记录。
我尝试使用 $exists
,但是根据 MongoDB documentation,此查询将返回等于 null 的字段。
$exists
does match documents that contain the field that stores the null value.
所以我现在假设我必须做这样的事情:
db.collection.find({ "fieldToCheck" : { $exists : true, $not : null } })
然而,每当我尝试这个时,我都会收到错误 [invalid use of $not]
有人知道如何查询这个吗?
最佳答案
使用 $ne
(表示“不等于”)
db.collection.find({ "fieldToCheck": { $ne: null } })
关于mongodb - 使用 MongoDB 检查字段是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19868016/