0

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

8
  • What exactly is the difference between the two? In the second one is there a typo (field names must be strings)? And what is the update that you say is not possible in the first schema? Commented Oct 5, 2012 at 12:03
  • 1
    In the second form items is not a valid Json object. It has to be a key-value pair. Commented Oct 5, 2012 at 12:05
  • @Thilo Actually i need to do update atomically , for example:i want to update "name" : "abcd" to "name":"cc" in items. Commented Oct 5, 2012 at 12:12
  • @GowtGM: Then you should probably ask about that. Maybe this helps: stackoverflow.com/questions/7425993/… Commented Oct 5, 2012 at 12:14
  • @Thilo Yes, but there is no possibility for doing that, i can only update using the index value but i need to do with the id. So i am looking for someother schema.. Commented Oct 5, 2012 at 12:16

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.