'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.



Solution 1:[1]

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}))
})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1