【问题标题】:UpdateMany in MongoDB running twice with $incMongoDB 中的 UpdateMany 使用 $inc 运行两次
【发布时间】:2020-08-22 19:13:50
【问题描述】:

感谢我在上一个问题 (UpdateMany mongodb documents with value from document) 中获得的帮助,我学会了如何使用 updateMany 进行递增。我写了以下代码:

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 数据库:

[{
    _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。

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

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

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

【问题讨论】:

  • 尝试删除.catch
  • 你是否同时使用异步和回调样式?
  • @D.SM ... 是吗?我不得不承认,我只是从教程中获取了这种格式。你建议在这里改变什么? (和/或我应该自己查找什么来调查它?)
  • 这两者是互斥的,这可以解释你的行为。选择一种风格。
  • 发生双重更新是因为当您同时使用回调(err, dbResp) => 和承诺.catch( 时,每个updateMany 都被执行。我从 Mongoose 文档中了解到这一点,但我现在似乎找不到链接。

标签: mongodb mongoose


【解决方案1】:

根据原始问题的每个 cmets,我可以通过删除 .catchawait 来解决这个问题。

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

【讨论】:

    【解决方案2】:

    只是想知道这是否更好。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      • 2018-01-07
      • 2021-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      相关资源
      最近更新 更多