0

I have a list in arango collection like below

{
"id":"123"
"studentlist": [
    "a",
    "b",
    "c"
  ]
}

Now based on some filter condition i have to replace particular element of list with the replacement set. In this example i want to replace b with [b1,b2], so that my collection should look like below

 {
        "id":"123"
        "studentlist": [
            "a",
            "b1",
            "b2",
            "c"
          ]
        }

how to do it using arango query.

krishna
  • 343
  • 2
  • 5
  • 19

2 Answers2

0

You can replace "studentlist" as the whole list.

UPDATE "<document_key>" WITH {
  "studentlist": [
    "a",
    "b1",
    "b2",
    "c"
  ]
} IN <collection_name>
Ömer Taban
  • 130
  • 1
  • 5
0

You have a lot of functions at your disposal to manipulate the data, e.g. using the array-modifying REMOVE_VALUE, APPEND and SORTED I massage the studentlist of the original data, and use the object-modifying MERGE to return the desired result.

LET data = {
"id":"123",
"studentlist": [
    "a",
    "b",
    "c"
  ]
}
LET list = SORTED(APPEND(REMOVE_VALUE(data.studentlist, "b"), ["b1", "b2"]))
RETURN MERGE(data, {studentlist: list})

Returns your desired

[
  {
    "id": "123",
    "studentlist": [
      "a",
      "b1",
      "b2",
      "c"
    ]
  }
]

as single element of the result list. This should get you started. You can use LET expressions in FOR queries, you can use sub-queries to assemble data, and so on... And then use @Ömer_Taban's answer to update the documents, e.g.

FOR d IN data
   FILTER <YOUR_CONDITIONS>
   LET list...
   ... 
   UPDATE d WITH list IN data
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Tom Regner
  • 6,856
  • 4
  • 32
  • 47