获取未连接到Neo4j中特定节点的节点
问题描述:
我想获取所有未连接到给定节点集合的节点。假设我有5个节点A,B,C,D,E。现在A-> B-> C与:Is_Friend
关系连接。现在我想要所有没有连接到A的节点(即D和E)。获取未连接到Neo4j中特定节点的节点
我尝试这样做查询,但它不工作
MATCH (a:Friend{name:"A"})-[:Is_Friend_Of*]->(b:Friend)
MATCH (c:Friend)
WHERE NOT (c)-[:Is_Friend_Of]->(b)
RETURN c
答
氏查询应该做你想要它,但是,我要提醒的是取决于你的数据库,你可能无法比拟的朋友数量的大小是什么得到很多比赛。
// match the single longest chain of friends in a :Is_Friend_Of relationship
// starting with 'A' that is possible
MATCH path=(a:Friend {name:"A"})-[:Is_Friend_Of*]->(b:Friend)
WHERE NOT (b)-[:Is_Friend_Of*]->()
WITH path
// then find the other friends that aren't in that path
MATCH (c:Friend)
WHERE NOT c IN nodes(path)
RETURN c
谢谢这个查询是完美的。 – Tg0206