Recently I run into a situation where I needed to fetch nested mongodb document data and wasted my 1 hour. If you are facing the same this is for you.
Here is the stackoverflow - Populate nested array in mongoose
I used this query -
public courseListRead(): Promise<ICourse[]> {
return this.courseModel.find().sort({
index: 1
}).populate('instructor').populate({
path: 'categories',
populate: {
path: 'posts',
model: 'CourseCategoryPost'
}
}).exec();
}
To populate nested documents you have to use .populate()
method like above.
I'm very beginner at MongoDB.
Thanks.
Top comments (2)
thank you.
What if we have multiply nested documents to populate