How can I write reflexive relationship query using Cypher
? For example, I have defined a directional relationship named follows
which is used between User
nodes. What I am looking for is finding the users that follow each other .
Here is what I have tried:
MATCH (x:User)-[:FOLLOWS]->(y:User), y-[:FOLLOWS]->x
Detail regarding Neo4j
:
Version: 3.5.2
Edition: Community
The terminology for this is a relationship, not a property, though yes you can look for users who follow each other. Something like:
MATCH (x:User)-[:FOLLOWS]->(y:User)
WHERE id(x) < id(y) AND (y)-[:FOLLOWS]->(x)
RETURN x, y
The id predicate here is to ensure you only see each pairing once, and not an additional time for the same pairing in opposite order.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.