1
votes

I'm very new with arangodb and like it very much but I'm struggling to create query statement. I have two collections (VA, VB) and edge between them (EC) I want find search Va with specific filter (not by _id) and then RETURN documents (VB) connected (via EC) where EC has specific property (ex. active: true)

In documentation I found only examples when vertex is already known. Thank you in advance, Jnl

1
FOR m IN VA FILTER m.name == 'some name' FOR v, e, p IN 1..1 ANY m GRAPH 'namedGraph' FILTER p.edges[0].active == true RETURN vJanul

1 Answers

1
votes

Yes, making a graph makes it a bit easier, but you can still query it without using a graph.

This is an example that will work just using the three collections directly:

FOR va IN VA
FILTER va.name == 'Bob'
    FOR e IN EC
    FILTER e._from == va._id && e.active == true
        FOR vb IN VB
        FILTER e._to == vb._id
        RETURN vb

If you want to use a graph, looks like you might have been testing one, then this will work:

LET myOrigin = FIRST(FOR d IN VA FILTER d.name == 'Bob' RETURN d._id)

FOR v, e, p IN 1..1 OUTBOUND myOrigin GRAPH 'GD'
FILTER p.edges[0].active == true
RETURN p.vertices[1]

The thing to note is that myOrigin needs to be an _id, which means when setting the value you use FIRST(...) when assigning the value. This ensures you get a single value back (the first one) and not an array.

Your example also works:

FOR m IN VA FILTER m.name == 'Bob' 
    FOR v, e, p IN 1..1 ANY m GRAPH 'GD' 
    FILTER p.edges[0].active == true 
    RETURN v 

The thing to note is that this example could match more than one document (as more than one document could have .name == 'Bob' and it will return all nodes in VB that match.

If you wanted the results to show which entry in VA was connected with VB, and had the option of having multiple VA values that match, this will help you:

FOR m IN VA FILTER m.name == 'Bob' 
    FOR v, e, p IN 1..1 ANY m GRAPH 'GD' 
    FILTER p.edges[0].active == true 
    RETURN {
        origin: m,
        connected_to: v 
    }

If you want to clean up the results, you can use UNSET to make the results better:

FOR m IN VA FILTER m.name == 'Bob' 
    FOR v, e, p IN 1..1 ANY m GRAPH 'GD' 
    FILTER p.edges[0].active == true 
    RETURN {
        origin: UNSET(m, '_key', '_rev'),
        connected_to: UNSET(v, '_key', '_rev') 
    }

It just removes those keys from the results sent to you in the query.

There are so many ways to retrieve data, just looking at different examples can really help get a feel for AQL.