简体   繁体   English

是否可以在mongo文档的嵌入式数组文档中添加对象列表?

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

I am having the mongo document as below: 我有如下的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") 
                          }
                         ] 
             }
         ]
  }

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 ... 对于第一个,我使用“ $ push”在菜单内添加项目 ...

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". 如果不知道应用程序的完整要求,很难知道架构何时“足够好”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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