MongoDB: updating an array in array MongoDB: updating an array in array mongodb mongodb

MongoDB: updating an array in array


After some more research, it looks like the only way to modify the array within an array would be with some outside logic to find the index of the element I want to change. Doing this would require every change to have a find query to locate the index, and then an update query to modify the array. This doesn't seem like the best way.

Link to a 2010 JIRA case requesting multiple positional elements...

Since I will always know the ID of the feature, I have opted to revise my document structure.

     {    "_id" : "v5y8nggzpja5Pa7YS",    "name" : "Example",    "display_name" : "EX1",    "groups" : [        {            "_id" : "s86CbNBdqJnQ5NWaB",            "name" : "Group1",            "display_name" : "G1",            "features" : {               "1" : {                       type     : "blog",                       name     : "[blog name]"                       owner_id : "ga5YgvP5yza7pj8nS"               },             }         },     ] },

With the new structure, changes can be made in the following manner:

db.orgs.update({_id: "v5y8nggzpja5Pa7YS", "groups._id": "s86CbNBdqJnQ5NWaB"}, {$set: {"groups.$.features.1.name":"Blog Test 1"}});