如何从猫鼬的嵌套对象中填充数据

时间:2019-11-09 17:41:46

标签: node.js mongodb express mongoose-schema

我的架构如下:

 const commentSchema = new Schema({
  name: String,
  surname: String,
  email: String,
  positions: [
    { detail: [{
  type: Schema.Types.ObjectId,
  ref: 'Position'
}],
progress: { type: String }, // keep track of the status
comment: { type: String } // keep track of internal notes
}],
});

字段详细信息包含mongo ID数组。

我正在尝试以下代码,但未获取填充数据:

const requirementsData = await Requirement.find({})
  .populate({
    path: "positions.detail",
    model: Position,
  })
  .exec(function(err, docs) {
           if(err){
               console.log("err====", err)
           }else{
               res.render('candidates/show', {candidate: docs})
           }
       });

1 个答案:

答案 0 :(得分:0)

如果我了解您的问题,您可以这样做

.populate({
path:'position',
populate:{path:'details'}
})