Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

72
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda