Harjeet Singh
Harjeet Singh

Reputation: 65

How to Add value in existing key value and update the collection in Mongodb?

I am using mongodb with meteor and I want to add the new amount in existing amount value key in mongodb and update the same collection. Can I do addition in mongodb query directly? payment.update({},$add{$set:{amount: amount}}); something like this so the new amount directly added into pervious amount;

Upvotes: 0

Views: 1959

Answers (2)

Pankaj Jatav
Pankaj Jatav

Reputation: 2184

You can use mongo $inc to increment the value like this:

payment.update({},{$inc:{amount: amount}});

This will increase amount of all doc by amount.

You can also update doc based on the condition like this:

payment.update({'_id':docId},{$inc:{amount: amount}});

Upvotes: 3

Atish
Atish

Reputation: 4425

Try this:

db.payments.update({"some":"condition"}, {$inc:{amount:new_amount}})

This will add new amount to old amount.

Upvotes: 2

Related Questions