Mongoose populate multiple nested documents
An interest alternative is pass an array to nested populate.
Course
.findById(req.params.courseId)
.populate({
path: 'classes',
model: 'Classroom',
populate: [{
path: 'instructors',
model: 'User'
},
{
path: 'location',
model: 'Location'
}]
})
Please try this one under mongoose v4, here is one good link about Population
Course
.findById(req.params.courseId)
.populate({
path: 'classes',
model: 'Classroom',
populate: {
path: 'instructors',
model: 'User'
}
})
.exec(function(err, cour) {
if (err)
console.log(err);
else {
Course.populate(cour,
{
path: 'classes.location',
model: 'Location',
}, function(err, c1) {
if (err)
console.log(err);
else
console.log(util.inspect(c1, { showHidden: true, depth: null }));
})
}
})