MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

findOneAndUpdate incrementa invece di aggiornare in mangusta

Puoi impostare il valore dinamico e passarlo nella query

function update_total_credit(total_amount, topup_value) {
//The flag value means your breakpoint where you decide which value should go in query, you can change on your requirement basis
  var flag = 1;    // increment by 1 every time
  if (!flag)
    flag = -1;     // decrement by 1 every time
  User.findOneAndUpdate({
      email: user_email
    }, {
      $inc: {
        credit: flag
      }
    },
    function(err, response) {
      if (err) {
        res.json(0);
      } else {
        res.json(response.credit);
      }
    });
}

Consulta il riferimento qui per $inc