我正在使用Mongoose用res_persons
查找_id
嵌套数据。但是它没有返回one array
,而是返回all
。这是代码和文档。
{
"documents": [],
"_id": "5d283957a9450309383aa349",
"event_name": "Main",
"res_persons": [
{
"_id": "5d283957a9450309383aa34b",
"name": "John",
"desig": "Software Developer"
},
{
"_id": "5d283957a9450309383aa34a",
"name": "Jane",
"desig": "Data Analyst"
}
],
"outcomes": [],
"__v": 0
}
现在,如果我触发此查询,
var query = { "res_persons": { $elemMatch:{_id:"5d283957a9450309383aa34a"} } };
//I also tried this but output is same.
//var query = { "res_persons._id": "5d283957a9450309383aa34a" };
return await this.eventModel.findOne(query).select("res_persons.name");
它返回此:
{
"_id": "5d283957a9450309383aa349",
"res_persons": [
{
"name": "John"
},
{
"name": "Jane"
}
]
}
如果我专心_id:"5d283957a9450309383aa34a"
,它应该只返回John
。为什么显示Jane
?
我在这里做错什么了吗?请纠正我。
谢谢!