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

129
Views
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 answers
Answer question

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 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!