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

211
Views
how to update 2nd schema using node js (mongdb)

I am trying to update my 2nd schema which having reference in first schema

ownerSchema.js

var ownerSchema = Schema({
    fname     : String,
    lname     : String,
    shopPlace : { 
                  type: Schema.Types.ObjectId,
                  ref: 'Shop'
                }
});
var Owner = mongoose.model('Owner', ownerSchema);

shopSchema.js

var shopSchema = Schema({
    shopName  : String,
    location  : String,
    startDate : Date,
    endDate   : Date
});
var Shop  = mongoose.model('Shop', shopSchema);

so I am trying to update my schema like this

const update = async (req, res) => {
  const { id } = req.params;
  let update = {};
  if (req.body.fname) update.fname = req.body.fname;
  if (req.body.lname) update.lname = req.body.lname;
  if (req.body.shopPlace.shopName) update.shopPlace.shopName = req.body.shopPlace.shopName;
  if (req.body.shopPlace.location) update.shopPlace.location = req.body.shopPlace.location;

   let newOwmer = new Owner.updateOne(
     { ownerId: id },
      {
        $set: update,
      },
      { runValidators: true }
     );
};

I am trying to update shop but its not working and where am i wrong i dont know

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  const update = async (req, res) => {
  const { id } = req.params;
  let update = {};
  let updateShop = {}
  if (req.body.fname) update.fname = req.body.fname;
  if (req.body.lname) update.lname = req.body.lname;
  if (req.body.shopPlace.shopName) updateShop.shopPlace.shopName = req.body.shopPlace.shopName;
  if (req.body.shopPlace.location) updateShop.shopPlace.location = req.body.shopPlace.location;

   // get shopPlace _id from Owner
   const { shopPlace } = await Owner.findOneAndUpdate(
     { _id: id },
      {
        $set: update,
      },
      { new:true}
     );
     // here you must have the document you just updated. Just recover the id of the shop to modify it in turn

    // update shop
    let newShop = await Shop.findOneAndUpdate(
     { _id: shopPlace },
      {
        $set: updateShop,
      },
      {new:true}
     );

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