【问题标题】:Decrementing a field of type number in mongodb and nodejs在 mongodb 和 nodejs 中减少类型为 number 的字段
【发布时间】:2018-09-28 06:07:26
【问题描述】:

我正在学习 mongodb。 我有一个名为 item 的集合,其中有一个 Number 类型的字段 piecesLeft,我想在用户订购此特定项目时减少此字段值。有什么帮助吗?

【问题讨论】:

    标签: node.js mongodb express


    【解决方案1】:

    使用$inc 运算符。

     db.yourcollection.update({ /* find options */ }, { $inc: {piecesLeft : -1 } });
    

    更新:

    db.yourcollection.update({
        _id: '572f16440a0dbb1e0cc02201', // Find updated item e.g. by _id
        piecesLeft: { $gt: 0 } // Update only if piecesLeft > 0
    }, {
        $inc: {
            piecesLeft: -1 // Increment by -1 
        }
    });
    

    【讨论】:

    • 选项是什么意思?你为什么不赞成我的问题?我是新手,正在努力学习。另外,我已经搜索并没有找到我的情况的答案。
    • 第一个参数是集合中更新记录的搜索条件。例如{ _id: '572f16440a0dbb1e0cc02201' }.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 2019-06-22
    • 2019-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多