I store balances of users in mongo. Now I want to mathematically add to an existing balance as in balance = balance + amount. I am looping over multiple values in a function which gets an array of balances that are to be updated. The following
UserModel.findOneAndUpdate({ address: accounts[c].address },
{ balance: accounts[c].amount },
(error, user) => {
// do stuff
}
);
What I want is something like:
{ balance: user.amount + accounts[c].amount },
I also tried to first read the value and in the callback function do another findOneAndUpdate, unfortunately the callback handler does not have the right index c and thus accesses the wrong element in accounts[c].amount.
balancefield withaccounts[c].amount?balance = balance + accounts[c].amount. NB: WithaddI mean the mathematical operation on numbers, not adding an element.