MongoDB:在包含对象数组的文档中查询完成情况
我在MongoDB:在包含对象数组的文档中查询完成情况,mongodb,Mongodb,我在文件夹集合中有以下文档: folders: [ { _id : 1, docs : [ { foo : 1, bar : undefined}, { foo : 3, bar : 3} ] }, { _id : 2, docs : [ { foo : 2, ba
文件夹
集合中有以下文档:
folders: [
{ _id : 1,
docs : [
{ foo : 1,
bar : undefined},
{ foo : 3,
bar : 3}
]
},
{ _id : 2,
docs : [
{ foo : 2,
bar : 2},
{ foo : 3,
bar : 3}
]
},
{ _id : 3,
docs : [
{ foo : 2},
{ foo : 3,
bar : 3}
]
},
{ _id : 4,
docs : [
{ foo : 1 }
]
},
{ _id : 5,
docs : [
{ foo : 1,
bar : null }
]
}
]
我需要能够查询没有未定义
值、空
值或docs.bar不存在值的文档。在上述情况下,查询应仅返回具有\u id:2
的文档。我目前有一个解决方案,但我想知道是否有更好的方法来查询文档
我当前的解决方案:
db.folders.find({$nor:[{“docs.bar”:{$exists:false}}]})
此
db.folder.find({"docs.bar": {$exists: true}, "docs.bar": {$ne: null}})
。。。将仅返回docs
数组中至少一个子文档具有填充的bar
属性的条目。注意:在这个查询中,两个谓词是AND,我认为这符合您的要求,它肯定会返回您提供的集合中带有\u id:2
的文档。证明我的解决方案返回了假阳性。然而,@glitch提供的数据是正确的。