简体   繁体   中英

Is it possible to add list of objects into embedded array document in mongo document?

I am having the mongo document as below:

  {
  "_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") 
                          }
                         ] 
             }
         ]
  }

Now i want to change this to :

       {
        "_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") 
                          }
                       } 
             }

           ]

   }

Is this possible in mongo? In the first schema, updating is not possible atomically becoz we can't use two "$" while updating deep layer. So i thought to change schema as same as second one, how can i achieve it?

For first one i have used "$push" for adding items inside menus ...

Any help would be great..

Your update is changing 'menu' object, so I would suggest changing the schema so that the menu is the top level document, rather than an array in another document.

Menu could either have a field referencing the top level object (in another collection) that it belongs to, or you can denormalize the fields of the top level object into each menu document.

Without knowing complete requirements of the application, it's difficult to know when the schema is "good enough".

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