1

I apologise if this is a dumb question but it is driving me nuts. I am fairly new to arango but usually I can muddle through and work it out, but not this time.

I want to insert another set of items into the list called 'type' in the data below. I can modify the contents of the sublist using an update but can't seem to work out how to append to the list.

FOR doc IN MAG_TEST filter doc._key == "3704086" INSERT {"Name":"Mary","datestart":"16 March 2017"} INTO doc.type

So this [ { "_key": "3704086", "_id": "MAG_TEST/3704086", "_rev": "3704109", "name": "Flip", "type": [ { "name": "flop", "sdate": "13 April 2016" }, { "name": "flap", "sdate": "14 April 2017" } ] } ]

becomes this [ { "_key": "3704086", "_id": "MAG_TEST/3704086", "_rev": "3704109", "name": "Flip", "type": [ { "name": "flop", "sdate": "13 April 2016" }, { "name": "flap", "sdate": "14 April 2017" }, { "name": "fling", "sdate": "18 April 2018" } ] } ]

Zafroth
  • 23
  • 3
  • Have a look at the [Array functions](https://docs.arangodb.com/3.0/AQL/Functions/Array.html) from the AQL documentation, you'll want to `UPDATE` the document, and you can `MERGE` the new documents with the existing array. Also, you might want to look at storing your dates in [another format](http://stackoverflow.com/questions/10286204/the-right-json-date-format) so they can be extracted and store timezone specific information. See if you can get it going. – David Thomas Apr 15 '17 at 15:42

1 Answers1

2

Here is an example of how you can use UPDATE with APPEND to update an attribute which contains an array of objects:

FOR doc IN MAG_TEST
FILTER doc._key == "3704086"
UPDATE doc WITH { type: APPEND(doc.type, {"Name":"Mary","datestart":"16 March 2017"})} IN MAG_TEST

In this example you:

  • FILTER to find the document you want to update
  • UPDATE the found document
  • Use WITH to show which attributes you want to update
  • Identify the type attribute as being updated
  • Use APPEND to add a new object to the existing value of doc.type
  • Specify the collection the updated document will be stored in, MAG_TEST

Hope that helps.

Documentation on the UPDATE command

Documentation on Array functions

Note: Don't use MERGE as I mentioned above, MERGE is used to merge the attributes of two documents into a single document. For joining arrays, APPEND is an option you can use. Going through the other Array Functions can help solve use cases you're sure to have when manipulating and fetching data from arrays.

David Thomas
  • 2,264
  • 2
  • 18
  • 20
  • Did this answer work for you? If so, remember to mark the answer as accepted, or let me know if you are still having issues. – David Thomas Apr 16 '17 at 23:57