假设我在某些收藏夹中有以下文件
{
"name": "Man1",
"Childrens": [
{
"name": "Children 1",
"age": "12"
},
{
"name": "Children 2",
"age": "18"
},
]
}
如何获取查询的输出,即获取孩子的年龄为18岁
{
"name": "Children 2",
"age": "18"
}
答案 0 :(得分:1)
您需要使用$elemMatch
查找文档,并使用$elemMatch
进行数据投影
db.collection.find({
Childrens: {
$elemMatch: {
age: "18"
}
}
},
{
Childrens: {
$elemMatch: {
age: "18"
}
},
_id: 0
})
OutPut-> here
[
{
"Childrens": [
{
"age": "18",
"name": "Children 2"
}
]
}
]