假设我有以下名为的集合survey
:
{ _id: 1, results: [ { product: "abc", active: true}, { product: "xyz", active: true} ] }
{ _id: 2, results: [ { product: "abc", active: false }, { product: "xyz", active: true } ] }
{ _id: 3, results: [ { product: "abc", active: false }, { product: "xyz", active: false } ] }
通过做:
db.survey.find(
{ results: { $elemMatch: { active: false } }
)
我将获得数组中至少一个对象为假的所有文档,即
{ _id: 2, results: [ { product: "abc", active: false }, { product: "xyz", active: true } ] }
{ _id: 3, results: [ { product: "abc", active: false }, { product: "xyz", active: false } ] }
但是我怎样才能只获得那些拥有所有关键的文档,active: false
即:
{ _id: 3, results: [ { product: "abc", active: false }, { product: "xyz", active: false } ] }
蒂亚..
所以在这里我得到了一个解决方案,它非常适合我的需要: