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})
}
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