Update field inside array with '$inc'

3,051 views
Skip to first unread message

diegomsana

unread,
Jul 16, 2009, 7:59:01 PM7/16/09
to mongodb-user
I'm trying to update a field that's inside an array in a document
using the modifier '$inc', but it's not working.

E.g, i want to increment "b". This:
>db.collection.update( { _id:2 }, { $inc: { c : { b : 1} } }); does not work and does not throw any error message.

Is it possible to have this working in a next version?

Eliot Horowitz

unread,
Jul 16, 2009, 8:42:34 PM7/16/09
to mongod...@googlegroups.com
Can you print out what the object look likes before and after that operation?

diegomsana

unread,
Jul 16, 2009, 10:27:34 PM7/16/09
to mongodb-user
It's the same before and after the operation:

{"_id" : 2 , "c" : [{"b" : 1}]}

Changing the document to this {"_id" : 2 , "c" : {"b" : 1}} doesn't
work either.

On 16 jul, 21:42, Eliot Horowitz <eliothorow...@gmail.com> wrote:
> Can you print out what the object look likes before and after that operation?
>

Eliot Horowitz

unread,
Jul 16, 2009, 10:29:14 PM7/16/09
to mongod...@googlegroups.com
I'm not sure this makes sense.
Do you think it should increment the b of every object in the array?
That seems a little weird to me.

Wouter

unread,
Jul 16, 2009, 10:49:42 PM7/16/09
to mongodb-user
Use dotnotated strings to reach into objects/arrays.

{"_id" : 2 , "c" : {"b" : 1}}

db.collection.update({_id:2},{ $inc : { 'c.b': 1 }});

or

{"_id" : 2 , "c" : [{"b" : 1}]}

db.collection.update({_id:2},{ $inc : { 'c.0.b': 1 }});

(note the extra 0 (zero) to identify the key of the object in the
array)

diegomsana

unread,
Jul 17, 2009, 8:47:38 AM7/17/09
to mongodb-user
Thanks Wouter, "db.collection.update({_id:2},{ $inc : { 'c.b': 1 }})"
worked.

Eliot: actually a real document would be something more like {"_id" :
3 , "c" : {"b" : 1, "c" : 1 , "d" : 2} , "a" : {"a" : 1, "b":1}}, by
doing such update i expect to change only field c.b: {"_id" : 3 ,
"c" : {"b" : 2, "c" : 1 , "d" : 2} , "a" : {"a" : 1, "b":1}}.

By the way, in php it's possible to use the dot notation. My code to
do this is like:
$doc = array("c.b" => 1);
$collection->update(array("_id"=>3), array('$inc' => $doc), true)

Don't remember seeing this possibility mentioned in the php docs.

Thanks.

Kristina Chodorow

unread,
Jul 17, 2009, 4:03:05 PM7/17/09
to mongod...@googlegroups.com
By the way, if you think a certain example would help other users and you have the time, please add it to the manual.  The PHP documentation lets uses add comments at the bottom of every page.

diegomsana

unread,
Jul 18, 2009, 7:43:09 AM7/18/09
to mongodb-user
sure, i'll do that soon.

On Jul 17, 5:03 pm, Kristina Chodorow <krist...@10gen.com> wrote:
> By the way, if you think a certain example would help other users and you
> have the time, please add it to the manual.  The PHP documentation lets uses
> add comments at the bottom of every page.
>

Hien Nguyen

unread,
Oct 30, 2016, 8:21:21 PM10/30/16
to mongodb-user
Hello,
Thanks Wouter for your answer. However if I have a boucle "for" and I have to replace 0 (the key of the object) by an index i. It will not word anymore with 'c.i.b'.
How can I solve the problem? 

Kevin Adistambha

unread,
Oct 30, 2016, 9:31:09 PM10/30/16
to mongodb-user

Hi Hien,

Hello,
Thanks Wouter for your answer. However if I have a boucle “for” and I have to replace 0 (the key of the object) by an index i. It will not word anymore with ‘c.i.b’.
How can I solve the problem?

Please note that you are replying to a thread that is seven years old. The problems this thread is discussing is likely not relevant to your issue.

Could you open a new thread with more details about:

  • What are you trying to achieve
  • What MongoDB version are you using
  • What have you tried so far
  • What error message(s) are you seeing

Best regards,
Kevin

Reply all
Reply to author
Forward
0 new messages