mongodb子元素查询 [英] mongodb subelement querying
本文介绍了mongodb子元素查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是示例
> 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
}
]
}
对此有任何过滤器命令吗?
is there any filter commands for this?
推荐答案
> db.test.aggregate(
{ $unwind: "$values" },
{ $match: { "values.check": true } }
).result
[
{
"_id" : ObjectId("50e22046dc278908f3a38a8e"),
"name" : "test",
"values" : {
"check" : true
}
}
]
这篇关于mongodb子元素查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文