Use $inc

11 views
Skip to first unread message

Martinus Martinus

unread,
Sep 27, 2012, 3:21:17 AM9/27/12
to mongod...@googlegroups.com
Hi,

How do I increment two fields at the same time using $inc? So for example I got this collection :

{
   "_id" : 1,
   "rat" : 10,
   "dog" : 15
}

and I want to use $inc and increment both "rat" and "dog" with value 10, so the result will be like below :
{
  "_id" : 1,
  "rat" : 20,
  "dog" : 25
}

How can I do this?

Thanks.

kristian kurniawan

unread,
Sep 27, 2012, 3:26:18 AM9/27/12
to mongod...@googlegroups.com
Hi Martinus,

You can use command like below :

db.collection_name.update({_id : 1}, {$inc : {rat : 10, dog : 10}}).

Best regards,

Kristian

2012/9/27 Martinus Martinus <marti...@gmail.com>

--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

Reply all
Reply to author
Forward
0 new messages