Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

196
Visualizações
(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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda