How to construct query to update nested array document in mongo?

Posted by GowtGM on Stack Overflow See other posts from Stack Overflow or by GowtGM
Published on 2012-10-05T09:12:32Z Indexed on 2012/10/05 9:37 UTC
Read the original article Hit count: 222

Filed under:
|
|

I am having following document in mongo,

  {
   "_id" : ObjectId("506e9e54a4e8f51423679428"),
   "description" : "ffffffffffffffff", 
   "menus" : [  
                {   
                  "_id" : ObjectId("506e9e5aa4e8f51423679429"),     
                   "description" : "ffffffffffffffffffff",  
                      "items" : [
                               {    
                              "name" : "xcvxc",     
                              "description" : "vxvxcvxc",   
                              "text" : "vxcvxcvx",  
                              "menuKey" : "0",  
                               "onSelect" : "1",    
                              "_id" : ObjectId("506e9f07a4e8f5142367942f") 
                              } ,
                              {     
                              "name" : "abcd",  
                              "description" : "qqq",    
                              "text" : "qqq",   
                              "menuKey" : "0",  
                               "onSelect" : "3",    
                              "_id" : ObjectId("507e9f07a4e8f5142367942f") 
                              }
                             ] 
                 },
                 {  
                    "_id" : ObjectId("506e9e5aa4e8f51423679429"),   
                    "description" : "rrrrr",    
                     "items" : [    {   
                                 "name" : "xcc",    
                                 "description" : "vx",  
                                 "text" : "vxc",    
                                 "menuKey" : "0",   
                                "onSelect" : "2",   
                              "_id" : ObjectId("506e9f07a4e8f5142367942f") 
                         } ]
                } 
            ]
   } 

Now , i want to update the following document :

 {  
     "name" : "abcd",   
    "description" : "qqq",  
    "text" : "qqq",     
    "menuKey" : "0",    
        "onSelect" : "3",   
    "_id" : ObjectId("507e9f07a4e8f5142367942f") 
  }

I am having main documnet id: "_id" : ObjectId("506e9e54a4e8f51423679428") and menus id "_id" : ObjectId("506e9e54a4e8f51423679428") as well as items id "_id" : ObjectId("507e9f07a4e8f5142367942f") which is to be updated.

I have tried using the following query:

db.collection.update({ "_id" : { "$oid" : "506e9e54a4e8f51423679428"} , "menus._id" : { "$oid" : "506e9e5aa4e8f51423679429"}},{ "$set" : { "menus.$.items" : { "_id" : { "$oid" : "506e9f07a4e8f5142367942f"}} , "menus.$.items.$.name" : "xcvxc66666", ...}},false,false);

but its not working...

© Stack Overflow or respective owner

Related posts about java

Related posts about query