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

143
Views
Correct way to do multiple updates on a MongoDB database

I have to receive an array of Objects and update each one of those, by their _id, in my MongoDB database.

I had one method that would insert one Object whenever I made an HTTP PUT, like this:

router.post('/bet', function(req, res){
  Bet.update(req.body)
    .then(dados => res.status(201).jsonp({dados: dados}))
    .catch(e => res.status(500).jsonp({error: 'erro'}))
})

Where update() is defined as:

module.exports.update = function(d){
    return Bet.findByIdAndUpdate({_id: d._id}, d, {new: true})
}

This works just fine whenever I need to update a single Bet, but to be able to update several, I created this:

router.put('/bets', function(req, res){
  req.body.forEach((obj) =>{
    Bet.update(obj)
    .then(dados => res.status(201).jsonp({dados: dados}))
    .catch(e => res.status(500).jsonp({error: e}))
  })
})

Is this correct? I'm having a lot of problems, that I honestly think are coming from doing a lot of different requests at the same time, but I'd like to start by guaranteeing that this first step is done properly.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

No, this is likely not the correct way. You are sending a response to the client the moment a single update is done.

I'd add a updateMany method like so:

module.exports.updateMany = function(arr){
    return Promise.all(arr.map(d => Bet.findByIdAndUpdate(d._id, d, {new: true})))
}

The method uses Array#map() to create an array of Promises, that resolve once the update is done and then uses Promise.all() to give the ability to wait for all promises to be done.

It resolves once all updates are done, or rejects when any update fails.

(Note: you don't have to specify a query object like {__id:id} for findByIdAndUpdate())

You can then call that from your bets route:

router.put('/bets', function(req, res){
  Bet.updateMany(req.body)
    .then(promiseArray => res.status(201).jsonp({dados: promiseArray}))
    .catch(e => res.status(500).jsonp({error: e}))
})
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!