Update Subdocument of Subdocument

3,548 views
Skip to first unread message

Faizan Sattar

unread,
Oct 26, 2011, 2:35:53 PM10/26/11
to mongodb-user
{ "_id" : ObjectId("4ea82f17f53c46c29230a887"), "cases" : [
{ "active" : true, "condition" : "Test", "id" : "4ea82f1786097",
"images" : [ { "caseid" : "4ea82f1786097", "path" : "api/case/
4ea82f1786097/image/4ea82f683cf25/", "id" : "4ea82f683cf25",
"active" : true }], "path" : "api/case/4ea82f1786097/", "title" :
"Face Front" } ], "user" : { "device_id" : "123" } }

Hello, I need some help updating the subdocument of a subdocument. I
want to change the active value of one of the images which is embedded
within the case document to false. Any help is appreciated. Thank You.

Marc

unread,
Oct 26, 2011, 3:55:04 PM10/26/11
to mongodb-user
You can do this one of three ways using the $set command.

> db.foo.update({"_id" : ObjectId("4ea82f17f53c46c29230a887")}, {$set:{"cases.0.images.0.active" : false}})

-- or --

> db.foo.update({"cases.id":"4ea82f1786097"}, {$set:{"cases.$.images.0.active" : false}})

-- or --

> db.foo.update({"cases.images.caseid":"4ea82f1786097"}, {$set:{"cases.0.images.$.active" : false}})

The "$" character acts as a pointer to the location in the array where
the document matching the query was found. Here is a link to the "The
$ positional operator" section of the update() section of the MongoDB
guide.
http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positionaloperator

Unfortunately, you will have to know the position of the sub-document
in at least one of the arrays. Mongo does not support multiple
positional operators. The following will not work:

> db.foo.update({"cases.id":"4ea82f1786097", "cases.images.caseid":"4ea82f1786097"}, {$set:{"cases.$.images.$.active" : false}})
can't append to array using string field name [$]
>

You may also find the Mongo Document on Dot Notation to be useful:
http://www.mongodb.org/display/DOCS/Dot+Notation+(Reaching+into+Objects)

Here is a link to a similar question asked by another user:
http://groups.google.com/group/mongodb-user/browse_thread/thread/8f1060590cf093c7/3d3d30d2bdb4acd0

Additionally, here is a link to a question regarding using the $push
modifier to add documents to nested Arrays.
http://groups.google.com/group/mongodb-user/browse_thread/thread/a144475778d84b5b/bed435419d176fae

Hopefully the above examples and documents will help you accomplish
what you need to do. Good Luck! If you have any additional
questions, the MongoDB community is here to help!

Faizan Sattar

unread,
Oct 26, 2011, 6:48:57 PM10/26/11
to mongodb-user
Can i do something like this?
db.foo.update({"cases.id":"4ea82f1786097"}, {$set:{"cases.$.images.
$.active" : false}})

On Oct 26, 3:55 pm, Marc <m...@10gen.com> wrote:
> You can do this one of three ways using the $set command.
>
> > db.foo.update({"_id" : ObjectId("4ea82f17f53c46c29230a887")}, {$set:{"cases.0.images.0.active" : false}})
>
> -- or --
>
> > db.foo.update({"cases.id":"4ea82f1786097"}, {$set:{"cases.$.images.0.active" : false}})
>
> -- or --
>
> > db.foo.update({"cases.images.caseid":"4ea82f1786097"}, {$set:{"cases.0.images.$.active" : false}})
>
> The "$" character acts as a pointer to the location in the array where
> the document matching the query was found.  Here is a link to the "The
> $ positional operator" section of the update() section of the MongoDB
> guide.http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positiona...
>
> Unfortunately, you will have to know the position of the sub-document
> in at least one of the arrays.  Mongo does not support multiple
> positional operators.  The following will not work:
>
> > db.foo.update({"cases.id":"4ea82f1786097", "cases.images.caseid":"4ea82f1786097"}, {$set:{"cases.$.images.$.active" : false}})
>
> can't append to array using string field name [$]
>
>
>
> You may also find the Mongo Document on Dot Notation to be useful:http://www.mongodb.org/display/DOCS/Dot+Notation+(Reaching+into+Objects)
>
> Here is a link to a similar question asked by another user:http://groups.google.com/group/mongodb-user/browse_thread/thread/8f10...
>
> Additionally, here is a link to a question regarding using the $push
> modifier to add documents to nested Arrays.http://groups.google.com/group/mongodb-user/browse_thread/thread/a144...

Marc

unread,
Oct 26, 2011, 7:18:17 PM10/26/11
to mongodb-user
Unfortunately, Mongo does not support multiple positional operators.

There is a Jira case requesting this feature for a future release.
Please vote for it!
https://jira.mongodb.org/browse/SERVER-831
Reply all
Reply to author
Forward
0 new messages