假设有这样一个 mongoose 数据库架构,
const teacherSchema = new mongoose.Schema({
name: {type: String,required: true,trim: true},})
const courseSchema = new mongoose.Schema({
name: {type: String,teacher: { type: 'ObjectId',ref: 'Teacher',required: true },})
const studentSchema = new mongoose.Schema({
name: {type: String,course: { type: 'ObjectId',ref: 'Course',})
const hobbySchema = new mongoose.Schema({
name: {type: String,student: { type: 'ObjectId',ref: 'Student',})
如何查询和返回特定教师的数据,同时嵌套他们教授的课程、参加课程的学生和他们的爱好? 注意:我使用 url 参数 (id) 找到老师
{
_id: "61040a6dec6d054128fa8eae",name: "John Doe",others: ...,courses: [
{
content: ...,students: [
{
content: ....,hobbies: [{
content:...
}
]
},{...other students taking the course}
]
},{...other subjects by John Doe}
]
}