mongodb modifying with express

Player.findByIdAndUpdate(player, {
    $set: {
        points1: {success: , total: }    // modify needed here
    }
}, function(err){
    if (err) {
        console.log(err);
    } else {
        console.log("UPDATED");
    }
});

I want to increase value of points1.success and points1.total. But I don't know the correct syntax of doing it. Please help me

Upvotes: 0

Views: 27

Answers (1)

TGrif
TGrif

Reputation: 5931

Maybe findByIdAndUpdate is not the best choice in this case (because we don't know original value of points1).

With the findById traditional approach, you can increment value like this:

Player.findById(player, (err, res) => {
  if (err) return console.log(err)
  res.points1.success++
  res.points1.total++
  res.save(err => {
    if (err) return console.log(err)
    console.log("UPDATED")
  })
})

But for sure, there are other ways to do the same.

Upvotes: 1

Related Questions