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

131
Visualizações
Duplicate object in MongoDB array using Mongoose

I have a Node.js app that is creating a 'unit' in the DB under a 'building' that is saving two 'units' inside the array included in 'building'. Both of these are identical in both data and created timestamp. Anyone have an idea why the below code would be causing this to happen? I am a bit confused at it. I don't see how it would be adding two objects to the array. The code is not being run twice, I checked that with console.log() and just looking at my API logs.

await Building.findOneAndUpdate(
        {buildingID},
        {"$push": {units: unitData}},
        (err, doc) => {
            if(err) {
                logger.error(`POST unit/new save error for unit: ${unitID} - error message: `, err)
                return res.json({success: false, err, message: 'Error saving new unit, please try again'})
            }   
            logger.debug('POST unit/new save() doc: ', doc)
            return res.json({success: true, message: `Successfully saved new unit with ID of: ${unitID}`, unitData})
        }
)
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I ended up figuring it out, when there is an await and a callback it will save the data twice. This is a documented issue on the MongoDB documentation.

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