1
votes

I thought that in Cypher relationship "<-[r]->" means "<-[r]- AND -[r]->, while relationship "-[r]-" means "<-[r]- OR -[r]->". But they returned the same result.

start n=node(1), m=node(2) create n-[:no_direction]-m;
start n=node(1), m=node(2) create n-[:left]->m;
start n=node(1), m=node(2) create n<-[:both_direction]->m;

start n=node(1), m=node(2) match n-[r]-m return r;
start n=node(1), m=node(2) match n<-[r]->m return r;

Both "match n-[r]-m return r" and "match n<-[r]->m return r" return 3 records. I thought that "match n-[r]-m return r" should return 3 records, and "match n<-[r]->m return r" should only return one record.

How do I distinguish relationships between <-[r]-> and -[r]- in Cypher query?

2

2 Answers

3
votes

Not sure, but I guess you're having some misunderstand on relationship's concepts here. A relationship in Neo4j is always directed, there's no exception from this rule.

In terms of querying you can disregard the fact that it's directed. Based on that the two statements are identical

start n=node(1), m=node(2) create n-[:RELTYPE]-m;
start n=node(1), m=node(2) create n<-[:RELTYPE]->m;

(the syntax ()<-[:TYPE]->() is IMHO not even documented anywhere).

MATCH n-[:RELTYPE]-m means 'match any relationship having type RELTYPE in any direction (incoming or outgoing) between 1 and 2'.

In your example above if you get 3 results this just means that there are 3 relations of any type and any direction between node 1 and 2.

1
votes

I would add to Stefan's correct answer that your line

start n=node(1), m=node(2) create n-[:no_direction]-m;

creates an INCOMING relationship from the first node's point of view, which might cause some confusion. This is just a default, since you provided no explicit direction and, as Stefan already said, relationships must be directed.