gpt4 book ai didi

node.js - Mongodb 和 nodejs 根据当前值更新值

转载 作者:行者123 更新时间:2023-12-04 07:45:25 26 4
gpt4 key购买 nike

我想用他自己的当前值和 + 更新点数,但没有工作,我也尝试了 + 60,但仍然无法工作,如何根据给定的新值更新基于他当前值的点数?

   const updateUserPoints = async (id) => {
await Users.findByIdAndUpdate({ _id: id }, {
points: points + 60
})
console.log("updated user wit id " + id)


}

最佳答案

使用 $inc

The $inc operator increments a field by a specified value and has the following form:

const updateUserPoints = async (id) => {
await Users.findByIdAndUpdate(id,
{ $inc: points: 60 } // current value +60
)
console.log("updated user wit id " + id)
}

关于node.js - Mongodb 和 nodejs 根据当前值更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67223219/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com