ArangoDB: filter by collection type in a traversal query

aql, arangodb

Solution

Another answer is using the `IS_SAME_COLLECTION` function as hinted by this SO answer:

FOR p IN TRAVERSAL(address, myEdge, @vertex_id, 'outbound', {paths:false})
  FILTER IS_SAME_COLLECTION('user', p.vertex._id)
   RETURN p.vertex._id)

or, since `TRAVERSAL` was removed in ArangoDB 3.0+ (see this answer and the migration guide), something like

FOR v IN 0..5 IN OUTBOUND @vertex_id myEdge
  FILTER IS_SAME_COLLECTION('user', v._id)
   RETURN v._id)

Problem

I am using ArangoDB 2.8 I am doing a traversal query that includes 2 different collections. However in my result I would like to get only a particular collection, but I don't see the way to filter by collection name. In my case I have `address` collection and `user` collection. In `address` collection I distinguish 3 levels as: {addressType: state}, {addressType: city} and {addressType: street}. Then I have an edge that links from `address` to `user` collection (state>city>street>user). I want to do a traversal (like in the code below) from an `address` (of any type) to the `user` (if any) and only return the collection of type `user` -for example if a street does not have a link to a user then return empty-. ``` For p in TRAVERSAL(address, myEdge, @vertex_id, 'outbound', {paths:false}) RETURN p.vertex._id) ```

Original source