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

198
Vistas
(Mongoose) I'm trying to make a single update query from an array of unique objects of unrelated items, but it doesn't work

To give you a quick overview, I am trying to get an update query in my express/node/mongoose application that could update x amounts of cards' data in a single query (where x could be either 0 or + infinite).

Here is what the JSON file looks like at the moment:

{
    "deck_id": "621f7c8cae4ba99300a29cef",
    "cards": [
        {
            "_id": "621f2f403e96942c55acfa71",
            "review_lapse": "test"
        },
        {
            "_id": "621f3d533e96942c55acfb1f",
            "review_lapse": "test"
        }
    ]
}

Each object represents a unique card (see _id key)

And here is the method in my controller (very inefficient as I use a foreach loop, obviously not very scalable if I had 10.000 cards:

module.exports.updateReviewLapse = updateReviewLapse =  async (req, res, next) => {
    try{
        let updates = req.body.review_update;   
            
            updates.forEach(async item => {
                    let card = await UserDeckCard.findOneAndUpdate(
                        { _id: item._id },
                        { $set: { 
                            review_lapse: item.review_lapse
                        }},
                        { new: true }
                    )
                    console.log(card)

            })


        return res.status(200).json("ok") 
    } catch (err){  return res.status(500).json({ message: "" + err  })}
}

I thought of changing the JSON file, yet that didn't give any convincing results, nor using Promise.all and I couldn't achieve anything with updateMany.

Thanks in advance for your help

about 4 years ago · Juan Pablo Isaza
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