因此,我试图查找所有具有字段集并且不为空的记录。
我尝试使用$exists,但是根据MongoDB文档,此查询将返回等于null的字段。
$exists与包含存储空值的字段的文档匹配。
所以我现在假设我必须做这样的事情:
db.collection.find({ "fieldToCheck" : { $exists : true, $not : null } })
但是,无论何时尝试,我都会收到错误消息[invalid use of $not] 有人知道如何查询此消息吗?
因此,我试图查找所有具有字段集并且不为空的记录。
我尝试使用$exists,但是根据MongoDB文档,此查询将返回等于null的字段。
$exists与包含存储空值的字段的文档匹配。
所以我现在假设我必须做这样的事情:
db.collection.find({ "fieldToCheck" : { $exists : true, $not : null } })
但是,无论何时尝试,我都会收到错误消息[invalid use of $not] 有人知道如何查询此消息吗?
Answers:
使用$ne(“不相等”)
db.collection.find({ "fieldToCheck": { $exists: true, $ne: null } })
find总是返回:符合条件的记录集合。
假设我们有一个如下所示的集合:
{
"_id":"1234"
"open":"Yes"
"things":{
"paper":1234
"bottle":"Available"
"bottle_count":40
}
}
我们想知道瓶子领域是否存在?
答:
db.products.find({"things.bottle":{"$exists":true}})
When <boolean> is true, $exists matches the documents that contain the field, including documents where the field value is null. 从文档。