Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

135
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda