2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

How to update a specific element from nested array elements using AQL of arangodb?

Here is my JSON document of arangodb:


    {
      "_id":"SOMECOLLECTION/someid"
      "_key":"someid"
      "ARRAYDATA": [
        {
          "DATA1": "my data1",
          "CREATED_BY": "some1",
          "CREATED_DATETIME": "2015-10-04T22:00:06.824+0530",
          "ID": "b43a8e4c-be91-42ff-b9c3-bf8f26e835e6"
        },
    	{
          "DATA1": "my data2",
          "CREATED_BY": "some2",
          "CREATED_DATETIME": "2015-10-04T23:00:06.824+0530",
          "ID": "b43a8e4c-be91-42ff-b9c3-bf8f22e835e4"
        },
    	{
          "DATA1": "my data3",
          "CREATED_BY": "some3",
          "CREATED_DATETIME": "2015-10-04T23:00:50.824+0530",
          "ID": "b43a8e4c-be91-42ff-b9c3-bf8f26e935e8"
        }
      ],
      "DATETIME": "2015-10-04T21:56:46.117+0530",
      "DESCRIPTION": "Some description"
    }


Now i want to update the element inside the array


{
      "DATA1": "my data3",
      "CREATED_BY": "some3",
      "CREATED_DATETIME": "2015-10-04T23:00:50.824+0530",
      "ID": "b43a8e4c-be91-42ff-b9c3-bf8f26e935e8"
}


How to do this now

userimage

You can try below query to update a specific element inside an array:


FOR q IN SOMECOLLECTION FILTER q._key == @keyId
LET altered = (FOR a IN q.ARRAYDATA LET upd_data = ((a.ID == @arrId && a.CREATED_BY==@by ? MERGE(a, {DATA1:@data, CREATED_DATETIME: @dateTime}) : a)) RETURN upd_data) ");
UPDATE q WITH { ARRAYDATA:  altered } IN SOMECOLLECTION


This will do it's work.

Answer is