这是例子
> db.test.insert({ name: 'test', values: [ { check: true }, { check: false } ] })
> db.find({ values.check: true })[0]
所以我既得到了真也得到了假check
:
{
"_id" : ObjectId("50e22046dc278908f3a38a8e"),
"name" : "test",
"values" : [
{
"check" : true
},
{
"check" : false
}
]
}
我想得到这个:
{
"_id" : ObjectId("50e22046dc278908f3a38a8e"),
"name" : "test",
"values" : [
{
"check" : true
}
]
}
有没有相关的过滤命令?
您可以使用$
投影运算符仅包含第一个values
与查询匹配的数组元素:
db.test.find({ 'values.check': true }, {name: 1, 'values.$': 1})
returns:
{
"_id": ObjectId("50e22046dc278908f3a38a8e"),
"name": "test",
"values": [ { "check": true } ] }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)