简体   繁体   中英

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

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...

The positional operator does not work on the number of levels you are trying to get it to work on ( https://jira.mongodb.org/browse/SERVER-831?focusedCommentId=22438&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel ) with menus.$.items.$.name and even if it did MongoDB query parser would have no idea what the other $ is from the find of the update .

You will need to pull out the items from the schema, update that seprately and then update the root document.

One good way of judging when queries should be done separately is to think that each menu sounds like a separate entity (or table in a relational database) as such you should probably work on updating those entites (or tables in a relational model) separately to the parent entity (table).

So first you would get out the main root document. Scroll across it's menus in client side and then $set that particular menu to the entire item you build on client side.

Edit

The way I imagine this work client side is (in pseudo code since my Java is a little rusty) by first getting that document in an active record fashion:

doc = db.col.find({ "_id" : { "$oid" : "506e9e54a4e8f51423679428"} , 
    "menus._id" : { "$oid" : "506e9e5aa4e8f51423679429"}});

Then you would iterate through the document assigning your values:

foreach(doc.menus as menu_key => menu){
    foreach(menu['items'] as key => item){
        if(item._id ==  { "$oid" : "506e9f07a4e8f5142367942f"}){
            doc.menus[menu_key][key][name] = "xcvxc66666"
        }
    }
}

And then simple save the doc after all changes are commited:

db.col.save(doc);

This is of course just one way of doing it and this way uses the activen record paradigm which I personally like. In this idea you would combine the find with everything else you need to modify on the document, building it up client side and then sending it all down as one single query to your DB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM