0

Here is the document i have

 {  
   "_id":"595a069cc3e06d0fc0e214c5",
   "username":"RReddy",
   "sellingpoints":[  
      {  
         "_id":"595b500a7c4dc0663a29af4c",
         "selling_p_settings":[  
            {  
               "_id":"595a069cc3e06d0fc0e21492",
               "heatoilvalue":"off",
               "dieselvalue":"on",
               "benzinvalue":"on"
            },
            {  
               "_id":"595a069cc3e06d0fc0e21493",
               "heatoilvalue":"on",
               "dieselvalue":"on",
               "benzinvalue":"on"
            }
         ]
      },
      {  
         "_id":"595b500a7c4dc663a29af4c",
         "selling_p_settings":[  
            {  
               "_id":"595a069cc3e06d0fc021492",
               "heatoilvalue":"off",
               "dieselvalue":"on",
               "benzinvalue":"on"
            },
            {  
               "_id":"595a069cc3e06dfc0e21493",
               "heatoilvalue":"on",
               "dieselvalue":"on",
               "benzinvalue":"on"
            }
         ]
      }
   ]
}

Wanted to achieve as follow as you could see in the following document same object is added to all the objects of an array

{  
   "_id":"595a069cc3e06d0fc0e214c5",
   "username":"RReddy",
   "sellingpoints":[  
      {  
         "_id":"595b500a7c4dc0663a29af4c",
         "selling_p_settings":[  
            {  
               "_id":"595a069cc3e06d0fc0e21492",
               "heatoilvalue":"off",
               "dieselvalue":"on",
               "benzinvalue":"on"
            },
            {  
               "_id":"595a069cc3e06d0fc0e21493",
               "heatoilvalue":"on",
               "dieselvalue":"on",
               "benzinvalue":"on"
            },
{  //new object
               "_id":"4545454545",
               "heatoilvalue":"on",
               "dieselvalue":"on",
               "benzinvalue":"on"
            }
         ]
      },
      {  
         "_id":"595b500a7c4dc663a29af4c",
         "selling_p_settings":[  
            {  
               "_id":"595a069cc3e06d0fc021492",
               "heatoilvalue":"off",
               "dieselvalue":"on",
               "benzinvalue":"on"
            },
            {  
               "_id":"595a069cc3e06dfc0e21493",
               "heatoilvalue":"on",
               "dieselvalue":"on",
               "benzinvalue":"on"
            },
{   //new object
               "_id":"4545454545",
               "heatoilvalue":"on",
               "dieselvalue":"on",
               "benzinvalue":"on"
            }
         ]
      }
   ]
}

I'm only using nodejs btw.

can i achieve this by MongoDB’s aggregation framework?

i am new to mongodb

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • The aggregation framework is a method for "querying". You are talking about "modifying" your documents in the collection permanently. Yes? – Neil Lunn Jul 07 '17 at 09:59
  • yes.How can i achieve this ? – Chaitanya Nekkalapudi Jul 07 '17 at 10:01
  • You cannot `$push` to multiple arrays in a single request. You will need to match each array element of the outer array and "loop" each one in order to `$push` to the inner one. But really, you should not be nesting arrays at all. It may be fine if all you ever do is `$push`, but because of the other limitations of the [positional `$` operator](https://docs.mongodb.com/manual/reference/operator/update/positional/) you cannot really "update" via `$set` or similar the items within an inner array. Nesting arrays is not good practice, so you should rethink the design. – Neil Lunn Jul 07 '17 at 10:08

0 Answers0