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

65
Views
Return Object with Properties of A Second Object referenced by ID with Mongoose

Example of a class object

{
students: [student._ids will go here],
_id: 61a683115340de04b3eb5448,
teacher: '6170936925956a143a98a02e',
title: '1st Social Studies',
}

is there a way for Mongoose to, in my case, populate the teacher with teacher.firstName, teacher.lastName, and teacher._id as an object when passing it to the front end? This would be a list of objects from a .find if that's significant.

router.route('/').get((req, res, next) => {
  classSchema.find((error, data) => {
    if (error) {
      console.log("bad class")
      return next(error)
    } else {
      console.log(data)
      res.json(data)
    }
  })
})

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use populate with a projection as the second param:

  classSchema.find((error, data) => {
    if (error) {
      console.log("bad class")
      return next(error)
    } else {
      console.log(data)
      res.json(data)
    }
  }, "students title teacher.firstName teacher.lastName")

or select:

  classSchema.find((error, data) => {
    if (error) {
      console.log("bad class")
      return next(error)
    } else {
      console.log(data)
      res.json(data)
    }
  }).select("students title teacher.firstName teacher.lastName")
about 4 years ago · Juan Pablo Isaza 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!