0
数组文件查询完成我有一个folders
集合内的下列文件:的MongoDB:在包含对象
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 }
]
}
]
我需要能够查询没有一个undefined
价值的文件,null
值,或不存在的值为docs.bar
。在上面的情况下,查询应该只返回文档_id: 2
。我目前有一个解决方案,但我想知道是否有更好的方式来查询文件。
我目前的解决方案:
db.folders.find({$nor: [{"docs.bar": { $exists: false }}]})
原来我的解决方案返回了一个误报。然而@glitch提供的[answer](https://stackoverflow.com/questions/45189590/mongodb-querying-for-completion-in-documents-containing-an-array-of-objects/45195904#45195904)却是正确的。 –