literallypenguin
literallypenguin

Reputation: 121

AQL Arango - Get Vertex and Neighbors using Edges

Coming from neo4j and new to Arango.

I am trying to query my data and build a corresponding data structure.

So I have something like:

    Circle A
    /       \
 Square A    Circle B 
            /     \         \
       Circle C   Square B  Square D

Circle are stored in a document collection. Square are stored in a document collection.

I then have two edge Collections HAS_CIRCLE and HAS_SQUARE which correspond appropriately.

I know I want Circle B and its neightbors - in a structure like so.

{
    circle: {data from Circle B},
    parents: [{data from Circle A}],
    children: [{data from Circle C}],
    squares: [{data from Square B}, {data from Square D}]
}

*Also to note I am not looking to nest this structure. Like when I want {data from Circle A} in parents - I don't expect this to also have parents, children, squares - just literally looking for the metadata contained in that node.

I know I can start like this... but I quickly get lost. And even when doing the basics - I can't seem to collect it up properly and associate the array to a key.

FOR c in Circle
    FILTER c.name === 'Circle B'
    FOR hc in HAS_CIRCLE
        FILTER hc._from === c._id

Upvotes: 0

Views: 1369

Answers (2)

literallypenguin
literallypenguin

Reputation: 121

much thanks to @dothebart - this did get me pointed in the right directions.

my query ended up looking like. still not 100% on if this is the most ideal but yields the results i was looking for.

FOR c IN Circle
  FILTER c.name == 'Circle B'
        RETURN {
            "circle" : c,
            "parents":  ( FOR parents IN INBOUND b._id HAS_CIRCLE RETURN parents )),
            "children": ( FOR children IN OUTBOUND b._id HAS_CIRCLE RETURN children ),
            "squares":  ( FOR squares IN OUTBOUND b._id HAS_SQUARE RETURN squares )
        }

Upvotes: 1

dothebart
dothebart

Reputation: 6077

You must have missed the trip to the graph documentation in ArangoDB. For sure you can use document queries with classical joins (like you tried) to do graph iterations and map it similar as you would do it in traditional SQL on any other RDBMS.

However, ArangoDB unveils its real graphing power, if you use the pattern matching traversals in queries like this:

FOR vertex, edge, path IN 
  1..5 
  OUTBOUND
  'circle/A'
  GRAPH circesAndSquares 
    FILTER edge.name == 'Circle B'
     RETURN {vertices: vertex, edges: edge, paths: path}

edge will contain the edge document of the current traversal step, vertex the vertex. FILTERing them will hide the non matching documents from the RETURN statement. Paths can be filtered on iteration depths, which then in term may abort a traversal:

FILTER path.edges[1].name == 'Circle B'

You can also filter on any iteration depth:

FILTER path.vertices[*].isValid == true

The examples in the documentation demonstrate howto work with named and anonymous graphs and howto insert your data into ArangoDB. ArangoDB has a special edge collection type which implicitely knows and enforces _from and _to attributes on edge documents - but aside of this restriction you can fill it with arbitrary documents.

You can also combine regular AQL queries with graph traversals, as demonstrated in this example.

Upvotes: 1

Related Questions