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

282
Views
Populate child collection

This is example from mongoose docs about populate:

const personSchema = Schema({
  _id: Schema.Types.ObjectId,
  name: String,
  age: Number,
  stories: [{ type: Schema.Types.ObjectId, ref: 'Story' }]
});

const storySchema = Schema({
  author: { type: Schema.Types.ObjectId, ref: 'Person' },
  title: String,
});

So, person has list of stories and when we fetch persons we can include stories by using populate('stories'). So far so good.

But in order for that to work, when creating Story, we need to add storyId to stories list in Person. I am coming from SQL background, where that does not need to be done, it would find related stories automatically based on authorId on Story.

So the question is, can it be done in same way here, without need to update stories property on Person?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I found a solution, its called virtual property:

AuthorSchema.virtual('posts', {
  ref: 'BlogPost',
  localField: '_id',
  foreignField: 'author'
});

This way I can populate posts in query without saving postIds in Author/Person schema.

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!