UpdateMany in MongoDB running twice with $inc

后端 未结 2 945
耶瑟儿~
耶瑟儿~ 2021-01-15 20:44

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

2条回答
  •  广开言路
    2021-01-15 21:17

    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 });)
    }
    

提交回复
热议问题