How to avoid cycle in neo4j cypher queries

I have friend-friend data model which has two relationships between any two friend nodes based on how one friend defines the other friend. For example, User "A" can define user "B" as 'FRIEND' and "B" can define "A" as 'BUDDY'. The problems is, when I try to get the 3rd degree of relationship of user "A", it returns user "B", where as the actual result should be "D" only.

MATCH(a:Users {first_name : "A"}) -[:BUDDY|FRIEND*3] -> (b)


MATCH (a)-[]-(b)-[]-(c)-[]-(d)
WHERE a.first_name="A" 

A quick fix for your second query would be:

MATCH (a)-[ ]-(b)-[ ]-(c)-[ ]-(d)
WHERE a.first_name="A" AND NOT b = d

However, I don't know if it solves your problem in the long term as you probably want to execute all kinds of queries without having to exclude all possibly wrong nodes.

Maybe for your first query you could get all nodes that are 3 hops away (some first collection of nodes) and then get all nodes that are 0-2 nodes away (a second collection of nodes) and then only return those nodes that are in the first but not in the second collection?

The response to this question may help you. It appears because the relationship can be reciprocal you don't need to specify the direction of your relationship which would make your first query. See if the proposed solution from that link helps.