MongoDB(Mongoose)如何使用$ elemMatch返回所有文档字段

根据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}}},function(err,school){…}

该操作返回以下文档:

{“_id”:1,“students”:[{“name”:“john”,“school”:102,“age”:10}]} {“_id”:3} {“_id”:4,学生“:[{”name“:”barney“,”school“:102,”age“:7}]}

但是我也需要提交学校的价值…

{“_id”:1,“school”:“School A”,“students”:[{“name”:“john”,“school”:102,“age”:10}]} {“_id”:3 ,“学校”:“学校C”} {“_id”:4,“学校”:“学校D”,“学生”:[{“名称”:“巴尼”,“学校” 7}]}

我找不到一个方法来实现这个…

http://docs.mongodb.org/manual/reference/method/db.collection.find/

如果指定了投影参数,则匹配文档只包含投影字段和_id字段。 您可以select排除_id字段。

但是…我强迫字段返回使用:

 schools.find({ zipcode: 63109}, {school: 1, students: { $elemMatch: { school: 102 } } }, function (err, school) { ...} 

而且都似乎正常工作…