Mongodb and nodejs update value base on the current value

Viewed 73

i want to update points with his own current value and + and is not working i tried + 60 aswell and still not working how can i update points base on his current value with the new value given?

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


  }
1 Answers

Use $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)
}
Related