2017-10-17 40 views
1

我有以下型号的团队如何填充物的嵌入数组中猫鼬

var team = new mongoose.Schema({ 
    supervisor: { type: mongoose.Schema.Types.ObjectId, ref: 'employee' }, // SupervisorId 
    commanders: [{ 
     employee: { type: mongoose.Schema.Types.ObjectId, ref: 'employee' }, //under supervisor 
     level: { type: Number, defult: 1 } 
    }] 
}); 

,同时从DB获取数据我能够填充上司而不是员工的指挥官阵列

我试过类似下面

db.team.findOne({ 
     supervisor: employeeId 
    }).populate({ 
     path: 'supervisor', 
     populate: { 
      path: 'commanders', 
      populate: { 
       path: 'employee', 
       model 
      } 
     } 
    }) 

需要帮助。

回答

1

尝试用此:

db.team.findOne({ 
     supervisor: employeeId 
    }).populate('supervisor commanders.employee')