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

154
Views
Updating mongodb document with new document id

I am trying to $push a document id to a collection but it seems to not working. I don't understand what I am doing wrong here. Please help me out. this is how user model looks like in my codebase

 const userSchema = new Schema({
    first_name: String,
    ...
    products: [{ type: Schema.Types.ObjectId, ref: 'Product' }]
});

const UserModel = model('User', userSchema);

product model

const productSchema = new Schema({
    name: string,
    ...
    user_id : [{ type: Schema.Types.ObjectId, ref: 'User' }]
});

const ProductModel = model('Product', productSchema);

controller

const savedUser = await new UserModel({ ...user }).save();
product.user_id = savedUser._id;
const savedProduct = await new ProductModel(product).save();
savedUser.update({ $push: { products: savedProduct._id } });
console.log(savedUser.populated('products')); // undefined
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

From the docs:

If the path was not populated, returns undefined.

You need to populate before populated can be used.

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!