Dovi
Dovi

Reputation: 853

ArangoDB: filter by collection type in a traversal query

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)

Upvotes: 3

Views: 1383

Answers (3)

You can also use OPTIONS:

WITH users
FOR v IN 0..5 IN OUTBOUND @vertex_id myEdge
  OPTIONS {vertexCollections: ['users']}
   RETURN v._id

Take a look here https://docs.arangodb.com/3.12/aql/graphs/traversals/

Upvotes: 0

sunside
sunside

Reputation: 8259

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)

Upvotes: 5

Dovi
Dovi

Reputation: 853

Well I had to find a solution, so here is mine (I know is not the best but worked for me):

What I do is split the _id by "/" and check whether the first part (collection name) is in ['user']

For p in TRAVERSAL(address, myEdge, @vertex_id, 'outbound', {paths:false})
  FILTER (SPLIT(p.vertex._id, "/", 1)[0]) IN ['user']
   RETURN p.vertex._id)

Upvotes: 1

Related Questions