Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

652
Views
How to join/populate 3 tables with one to many relationships in MongoDB/Mongoose?

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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!