1
votes

Here is my aql query giving me the pathways to a specific vertex:

FOR v, e, p IN 1..2 OUTBOUND @startVertex GRAPH 'courses'
    FILTER v._id == @target
    RETURN p.vertices

And it returns something like this

[
  [
    {
      "_key": "ADELPHI+UNIVERSITY==BIO+111",
      "_id": "courses/ADELPHI+UNIVERSITY==BIO+111"
    },
    {
      "_key": "BOSTON+UNIVERSITY==CAS+BI+108",
      "_id": "courses/BOSTON+UNIVERSITY==CAS+BI+108"
    }
  ],
  [
    {
      "_key": "ADELPHI+UNIVERSITY==BIO+111",
      "_id": "courses/ADELPHI+UNIVERSITY==BIO+111"
    },
    {
      "_key": "UNIVERSITY+OF+NEVADA-LAS+VEGAS==BIOL+196",
      "_id": "courses/UNIVERSITY+OF+NEVADA-LAS+VEGAS==BIOL+196"
    },
    {
      "_key": "BOSTON+UNIVERSITY==CAS+BI+108",
      "_id": "courses/BOSTON+UNIVERSITY==CAS+BI+108"
    }
  ]
]

When I use the FOR operator and try to iterate through p.vertices or p.vertices[*], it always flattens the array. Essentially I want to go through each array and return something for each.

Just like this python code:

lists = [[], [], []]
for l in lists:
    do_something_with_list(l)
1

1 Answers

3
votes

Option #1: You can use AQL variables together with a subquery (nested FOR) like in your updated sample below:

FOR v, e, p IN 1..2 OUTBOUND @startVertex GRAPH 'courses'
    FILTER v._id == @target

LET updatedVertices = (
    FOR v in p.vertices
    // here you can change the value of v as you want
    // in my sample here I convert a v object to a 
    // string representing it's _id
    RETURN v._id 
)

RETURN updatedVertices

Option #2: If you have a complex logic for the object transformation (i.e. if do_something_with_list from your sample is not that simple and can not be done with plain AQL), you can also use User Function. First step you have to register your user function, let's call it DO_SOMETHING_WITH_LIST. Then when you want to use it your AQL will look something like:

FOR v, e, p IN 1..2 OUTBOUND @startVertex GRAPH 'courses'
    FILTER v._id == @target

LET updatedVertices = DO_SOMETHING_WITH_LIST(p.vertices)

RETURN updatedVertices

Note, the user function above must accept and array and return an array.