获取未连接到Neo4j中特定节点的节点

时间:2017-06-02 10:52:52

标签: neo4j cypher

我想获得所有未连接到给定节点集的节点。假设我有5个节点A,B,C,D,E。现在A-> B-> C与:Is_Friend关系连接。现在我想要所有未连接到A的节点(即D和E)。

我尝试了这个查询,但它无效

MATCH (a:Friend{name:"A"})-[:Is_Friend_Of*]->(b:Friend) 
MATCH (c:Friend) 
WHERE NOT (c)-[:Is_Friend_Of]->(b)
RETURN c

1 个答案:

答案 0 :(得分:2)

Thi查询应该按照您的意愿执行,但是,我会提醒您,根据数据库中不匹配朋友数量的大小,您可以获得大量匹配。

// 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