how do you poulate a field in a mongoose schema which is refrenced to another schema and this schema is further refrenced to another schema?

Mongoose supports nested populating (see in the docs: https://mongoosejs.com/docs/populate.html#deep-populate). Note that you have to specify your model name of post schema where I´ve put the “post-model-name” placeholder.

So you could try something like this:

User.findById(req.user._id)
  .populate({ 
     path: 'viewed_posts',
     populate: {
       path: 'post',
       model: 'post-model-name'
     } 
  })
  .exec();

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top