UpdateMany in MongoDB running twice with $inc

后端 未结 2 944
耶瑟儿~
耶瑟儿~ 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:04

    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 })
        })
    }
    
    0 讨论(0)
  • 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 });)
    }
    
    0 讨论(0)
提交回复
热议问题