Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

arangodb AQL How to modify the value of the object in a nested array?

Tags:

arangodb

aql

I have document organized such way:

{
  "email": "[email protected]",
  "name": "tyler",
  "address": {
    "street": "Beijing Road",
    "zip": 510000
  },
  "likes": [
    "running",
    {
      "movie": "Star Wars"
    }
  ]
}

I have problem in modifying the value of "movie". Could you help me, how to modify the value with AQL?

Thanks!

like image 923
Tyler_li Avatar asked Oct 18 '22 04:10

Tyler_li


1 Answers

The following query should do. I put the explanation as comments inside the query. My example assumes the documents are present in a collection named collection:

FOR doc IN collection
  /* any filter condition to find the document(s) in question.
     you should make sure this uses an index if there is a substantial
     number of documents in the collection */
  FILTER doc.name == 'tyler' 

  /* enumerate the old likes and return them as is if they are not
     of type object or do not have a 'movie' attribute. If they are
     objects and have a 'movie' attribute, patch them with a 'movie'
     value of 'whatever' */
  LET newLikes = (
    FOR oldLike IN doc.likes 
      RETURN 
        (TYPENAME(oldLike) == 'object' && HAS(oldLike, 'movie')) ?
            { movie: 'whatever' } :
            oldLike
  ) 

  /* finally update the matching document(s) with the new likes */
  UPDATE doc WITH { likes: newLikes } IN collection
like image 82
stj Avatar answered Oct 21 '22 09:10

stj