Possible to populate two levels?

后端 未结 2 1648
故里飘歌
故里飘歌 2021-01-24 13:14

Say I have collections/documents like below:

question collection:

{
   _id: ObjectId(\"0000\"),
   title: \"test question\",
   survey: ObjectId(\"1234\"         


        
相关标签:
2条回答
  • 2021-01-24 13:50

    I guess the best is to nest in the top populate and make it as a object

     Court.
      findOne({ name: 'Val' }).
      populate({
        path: 'events',
        populate: { path: 'authorId' }
      }); 

    http://mongoosejs.com/docs/populate.html#deep-populate

    0 讨论(0)
  • 2021-01-24 13:56

    You need to do it in two steps; first populating survey, and then populating survey.user using a separate call to Model.populate:

    questions.findOne({_id: '0000'})
        .populate('survey')
        .exec(function(err, question) {
            questions.populate(
                question,
                { path: 'survey.user', model: 'User'},
                function(err, question) {...});
        });
    
    0 讨论(0)
提交回复
热议问题