Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

212
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda