Reputation: 1151
My schema looks like follows:
Teacher.js
const teacherSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
unique: true,
},
},
{
timestamps: true,
}
);
teacherSchema.virtual('students', {
ref: 'Student',
localField: '_id',
foreignField: 'teacher',
justOne: false
})
const Teacher = mongoose.model('Teacher', teacherSchema);
Student.js
const studentSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
},
teacher: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Teacher',
required: true,
},
},
{
timestamps: true,
}
);
menuItemSchema.virtual('courses', {
ref: 'Course',
localField: '_id',
foreignField: 'student',
justOne: false
})
const Student = mongoose.model('Student', studentSchema);
Course.js
const courseSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
},
student: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Student',
required: true,
}
},
{
timestamps: true,
}
);
mongoose.model('Course', coursesSchema)
In my router, I want to create an object that looks like follows and return it to the user:
{
<teacherName>: [
<studentName1>: [<courseName1>, <courseName2>, <courseName3>],
<studentName2>: [<courseName1>, <courseName2>, <courseName3>]
]
}
To perform this I need to do a multiple join between the 3 tables. What is the most efficient way to do this?
In the case of only two tables I do:
const teacherWithStudents = await (await Teacher.findOne({_id: req.params.teacherId}).populate('students')).execPopulate()
However, since there is another table, is there way to populate the student's courses as well so that they I can access all 3: the teacher, her students and each of their courses.
Upvotes: 2
Views: 1791
Reputation: 141
You can go deeper with .populate()
as follows:
Teacher.findOne({_id: req.params.teacherId})
.populate({
path: 'students',
populate: {
path: 'courses'
}
})
.exec(function(err, docs) {});
Further documentation: https://mongoosejs.com/docs/populate.html#deep-populate
Upvotes: 1