Atomic update to embedded array item

132 views
Skip to first unread message

Ash

unread,
Sep 24, 2010, 9:07:01 AM9/24/10
to mongodb-user
How can update an embedded array item using atomic update

e.g. posts -> comments

i would like to update a comment which has id of 2 by change the
comment name

Michael Dirolf

unread,
Sep 24, 2010, 9:44:28 AM9/24/10
to mongod...@googlegroups.com
Use the positional ($) modifier. So something like:
update({"comments.id": 2}, {$set: {"comments.$.name": "foo"}})

> --
> 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.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

Ash

unread,
Sep 24, 2010, 9:48:25 AM9/24/10
to mongodb-user
thanks,

can i do that particular update that will only affect a specific post?

Michael Dirolf

unread,
Sep 24, 2010, 9:50:04 AM9/24/10
to mongod...@googlegroups.com
Yes. Add something to the query document to select the post you want to update.

Ash

unread,
Sep 24, 2010, 9:51:45 AM9/24/10
to mongodb-user

thank you, it all works now :D
Reply all
Reply to author
Forward
0 new messages