0

I want to add a new array in a nested document which is the 2nd level of array in c collections. I want to add a new array in listSubnames e. Could you please help to write a query for the below requirement

          {  
            "Value":"Example2",
            "Value":"000000000000001",
            "sectionroles":["test11", "test12", "test13"],

         }

Below is my document

       {  
   "id":"Test",
   "name":"Test",
    "sections":[  
   {  
      "sectionname":"Example1",
      "sectionroles":["test1", "test2", "test3"],
      "subcategory":[ 
   "subname":"Coke"
     "listSubnames":[

         {  
            "Value":"demo",
            "ValueId":"123333333333",
           "sectionroles":["test1", "test2", "test3"],
         },
         {  
            "Value":"Example2",
            "Value":"6577544333333",
            "sectionroles":["test1", "test2", "test3"],

         }
      ]
      ]
       }
Subham
  • 420
  • 3
  • 11
  • 34
  • 1
    You might also see [Updating a Nested Array with MongoDB](https://stackoverflow.com/a/23577266/2313887). Not needed for this specific operation, but you should read it to realize that nesting arrays in general is not a really good idea. It's generally far better to consider a much flatter schema. For example in this case the `"subcategory.subname"` is better placed as a common property on each lower array item, rather than it's own higher level property. This flattens the schema and avoids common pitfalls. Nesting does not have the advantages you might have presumed it does. – Neil Lunn Sep 01 '19 at 00:55
  • Thank you for sharing the thread. I didn't exactly get the part "This flattens the schema " as I am new to mongo. Could you please rearrange the existing schema and show the correct schema which it should suppose to be... It would be very helpful to me. – Subham Sep 01 '19 at 02:10

0 Answers0