Reputation: 3276
I am building a follower
model with neo4j. User A [:Follows] -> user B and user B can do the same.
A is follower of B. Getting this information is straight forward. However I also want to get information at the same time if B [:Follows] -> A or not.
Is there a way to pull out this information?
Upvotes: 0
Views: 80
Reputation: 5385
this query will give you the followers of A and a true|false for each of them indicating if (s)he follows back
MATCH (ua:User {name:"bob"})-[:Follows]->(ub:User)
RETURN ub.name AS ubName,
EXISTS((ub)-[:Follows]->(ua)) AS followsBack
Upvotes: 1
Reputation: 921
You can do it by adding an OPTIONAL MATCH of the reverse path and then assigning this path to a variable. Then you test the length of the variable in the RETURN using a CASE statement to get the results
E.G.
MATCH (ua:User {name:"bob"})-[:Follows]->(ub:User {name:"Bill"})
OPTIONAL MATCH p = (ub)-[r:Follows]->(ua)
RETURN ua.name, ub.name, CASE WHEN LENGTH(p) > 0 THEN 'yes' ELSE 'no' END AS follows_back
This would return either: "bob", "bill", "yes" or "bob", "bill", "no"
Upvotes: 1