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

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

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