我正在使用Node.js,Koa,MongoDB,Monk。
当我在查询中使用$ elemMatch时,它工作正常,但在投影中它会被忽略。
我有这样的数据
{
_id: 1,
zipcode: "63109",
students: [
{ studentId: "1", name: "john", school: 102, age: 10 },
{ studentId: "2", name: "jess", school: 102, age: 11 },
{ studentId: "3", name: "jeff", school: 108, age: 15 }
]
}
{
_id: 2,
zipcode: "63110",
students: [
{ studentId: "1", name: "john", school: 102, age: 10 },
{ studentId: "4", name: "achilles", school: 100, age: 8}
]
}
{
_id: 3,
zipcode: "63109",
students: []
}
{
_id: 4,
zipcode: "63110",
students: [
{ studentId: "3", name: "jeff", school: 108, age: 15 }
]
}
我想要:
具有"邮政编码的文件" :" 63109"
并且学生数组应该只包含那个' studentId'是' 2' (如果存在)。
即。我想要这个:
{
_id: 1,
zipcode: "63109",
students: [
{ studentId: "2", name: "jess", school: 102, age: 11 }
]
}
{
_id: 3,
zipcode: "63109",
students: []
}
但是我得到了#34;学生的所有子文档"阵列。好像$ elemMatch投影被忽略了。
我正在使用的查询:
collection.find({"zipcode":"63109"}, {students:{$elemMatch:{studentId:"2"}}})
有没有办法实现这个目标?
P.S。我正在使用合作僧侣。