arangodb AQL Как изменить значение объекта во вложенном массиве?

У меня документ организован таким образом:

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

У меня проблема с изменением значения "фильм". Не могли бы вы мне помочь, как изменить значение с помощью AQL?

Спасибо!


person Tyler_li    schedule 12.10.2017    source источник


Ответы (1)


Подойдет следующий запрос. Я помещаю объяснение в виде комментариев внутри запроса. В моем примере предполагается, что документы присутствуют в коллекции с именем 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
person stj    schedule 12.10.2017
comment
Большое спасибо за подробный ответ. - person Tyler_li; 12.10.2017