Nimit Bedi Nimit Bedi - 2 months ago 24
Node.js Question

Arithmetic Operations in mongoDB using node.js

I am trying to update content in the mongoDB database using node.js for a REST API. I tried using mongoose functions like $add,$inc, $dec. But it is giving me an error saying "Unknown modifierenter image description here"

Following is the part of the code where the function is being called :
enter image description here
I am getting the correct outputs out of the console,log statements.

Where is the error?

Answer

$add is a aggregation operater not a normal operater

https://docs.mongodb.com/manual/reference/operator/aggregation/add/

SO use $inc instead of $add, it will work with negative and positive values both.

admin.update({"username": donar}, {$inc: {"amount": -amount}}, 
function (err, doc){
if (err) throw err;

 else (res.json(doc));
})

customer.update({"username": recepient}, {$inc: {"amount":amount}},
function (err, doc){
if (err) throw err;

else (res.json(doc));
})
Comments