0

I have mongo document like

  {
    "_id" : ObjectId("59ad2dbd7549de7c69ef0132"),
    "customerId" : "TELEFONICA",
    "organization" : "TELEFONICA",
    "description" : "",
    "events" : [ 
        {
            "id" : "abandonCartEvent",
            "name" : "Abandoned Cart Event",
            "attributes" : [ 
                {
                    "attrId" : "geoLocation",
                    "attrName" : "Customer Location",
                    "defaultValue" : "London"
                }, 
                {
                    "attrId" : "traits",
                    "attrName" : "Customer Traits",
                    "defaultValue" : "Sports Lover"
                }
            ]
        }, 
        {
            "id" : "resumeEvent",
            "name" : "Resume Event",
            "attributes" : [ 
                {
                    "attrId" : "geoLocation",
                    "attrName" : "Customer Location",
                    "defaultValue" : "London"
                }, 
                {
                    "attrId" : "traits",
                    "attrName" : "Customer Traits",
                    "defaultValue" : "Sports Lover"
                }
            ]
        }, 
       {
            "id" : "cancelEvent",
            "name" : "Cancel Event",
            "attributes" : [ 
                {
                    "attrId" : "geoLocation",
                    "attrName" : "Customer Location",
                    "defaultValue" : "London"
                }, 
                {
                    "attrId" : "traits_",
                    "attrName" : "Customer Traits",
                    "defaultValue" : "Sports Lover"
                }
            ]
        }
    ]
}

Here I want to update "attributes" array, i.e, I need to modify any one object of attributes array.

Can someone provide me solution for updating this nested document

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • Don't nest arrays. Read the documentation for the [positional `$` operator](https://docs.mongodb.com/manual/reference/operator/update/positional/) and understand why you don't do this. – Neil Lunn Sep 05 '17 at 08:21
  • You are directly hitting one of the current limitations of MongoDB, hope this helps https://stackoverflow.com/a/18574256/2655092 – whoopdedoo Sep 05 '17 at 08:21

2 Answers2

0

You have to wait for the next version of mongodb to this. Check this. Untill that time, redesign your document structure in a way so that it has one array(not double nested array) or traverse second array on application level.

barbakini
  • 3,024
  • 2
  • 19
  • 25
0

Above you given same document I mentioned output scenario,

  1. If are you going to update id : "abandonCartEvent" & attrId : "geoLocation" ,

update({ "customerId" : "TELEFONICA", "events.id":"abandonCartEvent"}, {$set: {"events.0.attributes.0.attrId": "CustLocation"}})

1a. If are you going to update attrId: "traits"

update({ "customerId" : "TELEFONICA", "events.id":"abandonCartEvent"}, {$set: {"events.0.attributes.1.attrId": "traidstTest"}})

  1. If are you going to update Id: "resumeEvent" arrtID ?

update({ "events.id":"resumeEvent"}, {$set: {"events.1.attributes.0.attrId": "GeoLocationSS"}})

  1. Same like if are you updating cancelEvent id,

update({ "events.id":"cancelEvent"}, {$set: {"events.2.attributes.0.attrId": "GeoLocationSTested"}})

I hope, you will understand the update on array fields.

If you have any queries ask me, or call(+91-9964058121)

Thanks