Search code examples
arangodbaql

Count distinct nodes from traversal in AQL


I am able to get all distinct nodes from a query, but not the count:

FOR v in 2..2 OUTBOUND "starting_node" GRAPH "some_graph"
return DISTINCT v._key

I want to get only the count of the result. I tried to use LENGTH(DISTINCT v._key) as suggested in the docs, but it's not a proper syntax of the AQL:

syntax error, unexpected DISTINCT modifier near 'DISTINCT v._key)'

The naive solution is to get all keys and count it on the client side, but I am curious how to do it on the server side?


Solution

  • Inspired by this blogpost http://jsteemann.github.io/blog/2014/12/12/aql-improvements-for-24/ I prepared the solution using LET:

    LET result = (FOR v in 2..2 OUTBOUND "starting_node" GRAPH "some_graph"
                      return DISTINCT v._key)
    RETURN LENGTH(result)
    

    It might be not optimal solution, but it works as I expected.