本文介绍了MongoDB 中的 UpdateMany 使用 $inc 运行两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢在我之前的问题中收到的帮助(UpdateMany mongodb 文档具有值从文档),我已经学会了使用 updateMany 递增.我写了以下代码:

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 following code:

ageAllCameraPriorities = async (req, res) => {
    await Camera.updateMany({ enabled: true }, { $inc: { processingPriority: 1 } }, (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 })
    })

        .catch(err => console.log(err))
}

这段代码的目的是运行一个如下所示的 mongoDB 数据库:

The intention of this code is to run through a mongoDB database that looks like this:

[{
    _id: 'ad07f',
    enabled: true,
    priority: 1,
    more: "more data"
},{
    _id: '9da0f',
    enabled: false,
    priority: 6,
    more: "more data"
},{
    _id: '35fas',
    enabled: true,
    priority: 3,
    more: "more data"
},{
    _id: '5f3ax',
    enabled: true,
    priority: 11,
    more: "more data"
}]

... 并增加每个启用文档的优先级";字段为 1.

... and increase each enabled document's "priority" field by 1.

然而,上面的代码将值增加了 2.

The above code, however, increases the value by 2.

此外,如果我更改为 $inc: { priority: 3 } 值会增加 6.在我看来,某些东西可能会运行两次.但是我直接使用邮递员到达端点,上面的代码是我的全部功能.第二个增量来自哪里?

Further, if I change to $inc: { priority: 3 } the value increases by 6. It seems to me that something is likely running twice. But I'm hitting the endpoint directly with postman and the above code is the entirety of my function. Where is the second increment coming from?


很明显,第二次运行发生在这条线上:return res.status(200).json({ success: true, data: dbResp })因为我可以把除那条线以外的所有东西都拉出来,它会增加 2.如果我删除那条线,它只会增加 1.(但 API 端点永远不会返回任何东西......所以这不好)


It's clear the second running is happening during this line:return res.status(200).json({ success: true, data: dbResp })Because I can pull everything out other than that line and it will increment by 2. If I remove that line, it will only increment by 1. (but the API endpoint never returns anything... so that's not good)

推荐答案

只是想知道这是否更好.

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

这篇关于MongoDB 中的 UpdateMany 使用 $inc 运行两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 15:19
查看更多