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

196
Vistas
Mongoose delete other objects after update

I'm having trouble with my update query on mongoose. I'm not sure why other objects get deleted after I update a specific object. the code works when I update but after that, the rest of the objects inside the array are getting deleted/removed. Literally, all of the remaining objects get deleted after the update request.

export const updateProduct = async (req,res) => {
     const { id } = req.params;
 
     try {
         if(!mongoose.Types.ObjectId.isValid(id)) return res.status(404).json({ message: 'Invalid ID' });
 
         await OwnerModels.findOneAndUpdate({'_id': id, store:{$elemMatch: {productname: req.body.store[0].productname }}},
             {$set: 
                 {
                     store: 
                         {
                             productname: req.body.store[0].productname,
                             price: req.body.store[0].price,
                             quantity: req.body.store[0].quantity,
                             categoryfilter: req.body.store[0].categoryfilter,
                             description: req.body.store[0].description,
                             timestamp: req.body.store[0].timestamp                        
                         }
                 }
         }, // list fields you like to change
         {'new': true, 'safe': true, 'upsert': true});
 
     } catch (error) {
         res.status(404).json(error)
     } }
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I'm not sure why other objects get deleted after I update a specific object.

Because you are updating the whole object and it will replace the existing store array of object in the database,

You need to use arraFilters, and upsert is not effective in array of object updated, so i have commented,

await OwnerModels.findOneAndUpdate(
    {
        '_id': id, 
        store:{
            $elemMatch: {
                productname: req.body.store[0].productname 
            }
        }
    },
    {
        $set: {
            store: {
                "store.$[s].productname": req.body.store[0].productname,
                "store.$[s].price": req.body.store[0].price,
                "store.$[s].quantity": req.body.store[0].quantity,
                "store.$[s].categoryfilter": req.body.store[0].categoryfilter,
                "store.$[s].description": req.body.store[0].description,
                "store.$[s].timestamp": req.body.store[0].timestamp
            }
        }
    },
    {
        'arrayFilters': [
            { "s.productname": req.body.store[0].productname }
        ],
        'new': true, 
        'safe': true, 
        // 'upsert': true
    }
);
over 4 years ago · Santiago Trujillo 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