Thanks to the help received in my previous question (UpdateMany mongodb documents with value from document), I have learned about incrementing using updateMany. I wrote the
Per comments on the original question, I was able to solve this by removing the .catch
and the await
.
ageAllCameraPriorities = async (req, res) => {
Camera.updateMany( { enabled: true },
{ $inc: { processingPriority: req.params.amount } },
{},
(err, dbResp) => {
if (err) {
return res
.status(400)
.json({ success: false, error: "Status 400, unable to age camera priorities" + err })
}
if (!dbResp.n) {
return res
.status(404)
.json({ success: false, error:'No enabled cameras found to age' })
}
return res
.status(200)
.json({ success: true, data: dbResp })
})
}
Was just curious to know if this is better.
ageAllCameraPriorities = async (req, res) => {
await Camera.updateMany(
{ enabled: true },
{ $inc: { processingPriority: 1 } }
).then((dbResp) => {
if (!dbResp.n) {
return res
.status(404)
.json({ success: false, error: `No enabled cameras found to age` })
}
return res.status(200).json({ success: true, data: dbResp })
}).catch(err =>
console.log(err);
return res.status(400).json({ success: false, error: "Status 400, unable to age camera priorities" + err });)
}