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

194
Views
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 answers
Answer question

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