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:
{
"review_update": [
{
"_id": "621f2f403e96942c55acfa71",
"review_lapse": "bite"
},
{
"_id": "621f3d533e96942c55acfb1f",
"review_lapse": "bite"
}
]
}
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