我有像这样的数据库
{
"name": "Tom",
"cars": [
{
"name": "Shirley",
"kind": "Bugatti"
},
{
"name": "Jessica",
"kind": "Maserati"
},
]
}
我有这样的查询
db.people.findOne({"name: "Tom"});
我只想返回"name": "Jessica"
{
"name": "Jessica",
"kind": "Maserati"
}
我该怎么做?
我试过
db.people.findOne({"name": "Tom"}, {"cars.name": 1});
db.people.findOne({"name": "Tom"}, {"cars.name.Jessica": 1});
我试图阻止返回整个事情,然后对所有内容进行排序。
答案 0 :(得分:1)
db.people.findOne({"name": "Tom"}, {"cars": { $elemMatch : { "name": "Jessica"}}});
{
"_id" : ObjectId("526453188257d12214ea287d"),
"cars" : [
{
"name" : "Jessica",
"kind" : "Maserati"
}
]
}