我已经搜查高和低,但无法弄清楚如何形成下面填入查询时,先在这里是我的模型:猫鼬填充多重嵌套文件
const CourseSchema = new Schema({
classes: [{ type: Schema.Types.ObjectId, ref: 'Classroom' }]
});
const ClassSchema = new Schema({
location: { type: mongoose.Schema.Types.ObjectId, ref: 'Location' },
instructors: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }],
});
我有得到一个端点当然,但我想填充classes
字段和location
和instructors
字段classes
。现在,我可以在classes
或location
填充教师字段,但我无法同时填充它们两个。这就是我现在:
Course
.findById(req.params.courseId)
.populate({
path: 'classes',
populate: {
path: 'instructors',
model: 'User'
}
})
如何我也填充在classes
的location
场?
谢谢。
使用这个插件https://www.npmjs.com/package/mongoose-deep-populate – styopdev
我不知道,如果这个工程:'.populate({路径: '班',填充:{路径:'教师位置'}})'。 – robertklep
@robertklep,我测试你的代码,只有'location'被填充正确。然而,“教官”是空的,它不是人口稠密的...... – zangw