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

111
Views
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 answers
Answer question

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 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!