// 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
match(a:Friend {name: "A"})-[:Is_Friend_Of*]-(b)
with collect(distinct b) as all_connected_to_a
match(n:Friend) where not n in all_connected_to_a return n;
2条答案
按热度按时间tquggr8v1#
这个查询应该做你想做的事情,但是,我会提醒你,根据你数据库中不匹配的朋友数量的大小,你可能会得到很多匹配。
字符串
htrmnn0y2#
试试这个查询:
字符串
它首先得到所有连接到
(a:Friend {name: "A"})
的节点的集合(包括节点a,因为模式-[:Is-Friend_Of*]-
不是有向的)。然后返回所有不在该集合中的节点,即它们没有通过:Is_Friend_Of
类型的关系连接到(a:Friend {name: "A"})
。