我已经四处搜寻,但无法想出如何构建以下的填充查询,首先这是我的模型:
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
字段以及classes
中的location
和instructors
字段。目前,我只能填充classes
中的instructors
字段或location
字段,但不能同时填充它们。现在,以下是我的代码:
Course
.findById(req.params.courseId)
.populate({
path: 'classes',
populate: {
path: 'instructors',
model: 'User'
}
})
我还能如何填充classes
字段中的location
? 谢谢。
.populate({ path : 'classes', populate : { path : 'instructors location' } })
。 - robertklep