Arangodb AQL How to change the value of an object in a nested array?

I have a document organized like this:

{
  "email": "tyler_li@126.com",
  "name": "tyler",
  "address": {
    "street": "Beijing Road",
    "zip": 510000
  },
  "likes": [
    "running",
    {
      "movie": "Star Wars"
    }
  ]
}

I have a problem with changing the value of "movie". Could you help me how to change a value using AQL?

Thank!

+4
source share
1 answer

The following query should do. I explained this as comments inside the request. My example assumes the documents are in a collection with the name 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
+2
source

Source: https://habr.com/ru/post/1687341/


All Articles