我当前的数据库看起来像这样
{
"_id" : ObjectId("5a6816bb8b38336ee1e842b4"),
"date" : ISODate("2018-01-26T00:00:00.000+0000"),
"examType" : ObjectId("5a65a84d13ea9dbfb52125df"),
"className" : "8",
"section" : "f",
"subject" : "Language",
"students" : [
{
"comments" : "good",
"marks" : NumberInt(100),
"studentId" : ObjectId("5a56fd3a02a50271d233a92f"),
"_id" : ObjectId("5a6821168b38336ee1e842ba")
},
{
"comments" : "nice",
"marks" : NumberInt(100),
"studentId" : ObjectId("5a65ce5cc0533638e4e2df3f"),
"_id" : ObjectId("5a6821168b38336ee1e842b9")
}
]
}
我想填充两个ID ... 一个是 examType ,另一个是 student student 对象数组中的 studentId 。
我想要的输出是
{
"_id" : ObjectId("5a6816bb8b38336ee1e842b4"),
"date" : ISODate("2018-01-26T00:00:00.000+0000"),
"examType" : {
"_id": "5a65a84d13ea9dbfb52125df",
"name": "annual",
"__v": 0,
"totalMarks": 100
},
"className" : "8",
"section" : "f",
"subject" : "Language",
"students" : [
{
"comments": "good",
"marks": 100,
"student": {
"_id": "5a56fd3a02a50271d233a92f",
"firstName": "test",
"lastName": "sample",
"dob": "2017-12-31T18:30:00.000Z",
},
"_id" : ObjectId("5a6821168b38336ee1e842ba")
},
{
"comments" : "nice",
"marks" : NumberInt(100),
"studentId" : {
"_id": "5a56fd3a02a50271d233a93f",
"firstName": "abcd",
"lastName": "efgh",
"dob": "2017-12-31T18:30:00.000Z",
},
"_id" : ObjectId("5a6821168b38336ee1e842b9")
}
],
}
如何在对象数组中为examType和studentId实现这个多重填充?
并且还需要与主题分组
答案 0 :(得分:0)
如果你使用的是Mongoose> = 3.6,你可以传递一个以空格分隔的路径名字符串来填充:
<tablename>.find()
.populate('examType students.studentId')
.exec(function (err, results) {});