Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

137
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda