如何在mongoose中填充对象的嵌入数组

时间:2017-10-17 10:52:35

标签: node.js mongodb mongoose

我有团队的以下模型

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.team.findOne({
        supervisor: employeeId
    }).populate({
        path: 'supervisor',
        populate: {
            path: 'commanders',
            populate: {
                path: 'employee',
                model
            }
        }
    })

需要帮助。!!

1 个答案:

答案 0 :(得分:1)

试试这个:

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