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

134
Views
Unable to sync new data and update the duplicate one with new data

I get updated and new products which i have to save in my db. The data which i recieve should be like if there is a new SKU it gets added and if there is an SKU whose details are updated then it should be updated. For eg if there is no SKU then it gets deleted in db as well

The for loop which i use below gives me updated products but it donot delete the pervious one for eg i there is and sku name ABC and i rename it to DEF then both the entries remain in my db, i want DEF to replcace ABC

In mappedProducts i get all the products

let updatedProducts = mappedProducts.filter(p => savedSKUs.indexOf(p.sku) != -1);
let newProducts = mappedProducts.filter(p => savedSKUs.indexOf(p.sku) == -1);
await dbProducts.insertMany(newProducts);
               
                for(let i= 0; i< updatedProducts.length; ++i){
                    let currentData = updatedProducts[i];
                    let operatingSKU = currentData.sku;
                    //await dbProducts.findOneAndDelete({sku: currentData.sku});
                    await dbProducts.findOneAndUpdate({
                        SGID: req.decoded.SGID,
                        sku: operatingSKU
                    },{$set: currentData})
                }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

it's your filter that blocks the update because it has both values so it can't find the object to update.
Your filter should only contain the SGID value and the update one the sku value

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!