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

112
Visualizações
hide a field of virtual population mongoose

i have a comment collection and a post collection, and post schema are like this:

postSchema.virtual("comments", {
  ref: "Comment",
  localField: "_id",
  foreignField: "post",
})

and when i want to see comments of a post i use this:

   const post = await Post.findById(req.params.id);
    await post.populate("comments");
    res.send(post.comments);

i dont want to get email field of post.comments. i tried foreach delete key and it didnt work tnx for helping me

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use the regular field selection method for .populate with virtuals too:

await post.populate("comments", "-email");
// or if you want to exclude more than one field:
// await post.populate("comments", "-email -field2 -field3");

If you want to use positive selection (which fields should be included in the result), make sure that you always include the foreignField of the virtual (in this case post):

await post.populate("comments", "post field2 field3");
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