【发布时间】:2014-02-01 06:09:19
【问题描述】:
根据 MongoDB 文档 (http://docs.mongodb.org/manual/reference/operator/projection/elemMatch/):
{
_id: 1,
school: "school A",
zipcode: 63109,
students: [
{ name: "john", school: 102, age: 10 },
{ name: "jess", school: 102, age: 11 },
{ name: "jeff", school: 108, age: 15 }
]
}
{
_id: 2,
school: "school B",
zipcode: 63110,
students: [
{ name: "ajax", school: 100, age: 7 },
{ name: "achilles", school: 100, age: 8 },
]
}
{
_id: 3,
school: "school C",
zipcode: 63109,
students: [
{ name: "ajax", school: 100, age: 7 },
{ name: "achilles", school: 100, age: 8 },
]
}
{
_id: 4,
school: "school D",
zipcode: 63109,
students: [
{ name: "barney", school: 102, age: 7 },
]
}
发射:
schools.find({ zipcode: 63109}, {students: { $elemMatch: { school: 102 } } },函数(错误,学校){ ...}
该操作返回以下文档:
{“_id”:1,“学生”:[{“姓名”:“约翰”,“学校”:102,“年龄”: 10 } ] } { "_id" : 3 } { "_id" : 4, "学生" : [ { "name" : “巴尼”,“学校”:102,“年龄”:7 } ] }
但我也需要提交学校的价值...
{ "_id" : 1, "school": "School A", "students" : [ { "name" : "john", "school" : 102, "age" : 10 } ] } {“_id”:3,“学校”:“学校 C”} {“_id”:4,“学校”:“学校 D”,“学生”:[{“名称”: “巴尼”,“学校”:102,“年龄”:7 } ] }
我找不到实现这一目标的方法...
【问题讨论】: