Neo4j:如何获取节点列表的相互节点

Neo4j: How to get mutual nodes for list of nodes

我有这个密码:

MATCH path = (me:Person {uuid: '{my_id}'})-[:friendship*2]->(p:Person)-[:courses]->(c:Course)
WHERE p.uuid<>'{my_id}'
RETURN distinct(c) as courses

此代码return *2 关系中每个人的所有课程。但是我怎样才能只为这些人获得类似的课程呢?

狐狸例子: Person1 有:Course1、Course2、Course3。

Person2 拥有:Course2、Course3、Course4

我的代码会return[Course1, Course2, Course3, Course4],但我需要[Course2, Course3](仅相互)

感谢您的回答。

更新 1: 这是我的版本:

MATCH (me:Person {uuid: '{my_id}'})-[:friendship*2]->(p:Person)-[cr:courses]->(c:Course)
WITH collect(p) as persons, collect(cr) as courses_rel, g, collect(c) as courses
WHERE p.uuid<>'{my_id}' and all(rel in courses_rel WHERE all(person in persons WHERE startNode(rel) = person ))
RETURN courses

此代码有效,但 return 未经过滤的错误数据

更新 2:

我需要学习 3 门课程:Ackerman, 27 Pines Golf Course, 18 Mile Creek Golf

那你还需要匹配你的课程。您可以重复使用相同的变量来只获取相互课程:

MATCH (c:Course)<-[:courses]-(me:Person {uuid: '{my_id}'})-[:friendship*2]->(p:Person)-[:courses]->(c)
WHERE p.uuid<>'{my_id}'
RETURN distinct(c) as courses

首先,找到第 2 级的所有不同的人以及他们拥有的所有不同的课程。使用 WITH.

将这些值传递到查询的下一部分

然后对于这些课程中的每一门,检查 all 人们是否拥有它。

MATCH path = (me:Person {uuid: '{my_id}'})-[:friendship*2]->(p:Person)-[:courses]->(c:Course)
WHERE p.uuid<>'{my_id}'
WITH collect(distinct(c)) as courses, collect(distinct(p)) as persons
MATCH (c:Course)
WHERE c in courses and all(p in persons where (p)-[:courses]->(c))
RETURN c as courses

编辑: 如果您需要至少有两个朋友添加的课程,则计算每个课程的人数并过滤掉。

MATCH path = (me:Person {uuid: '{my_id}'})-[:friendship*2]->(p:Person)-[:courses]->(c:Course)
WHERE p.uuid<>'{my_id}'
WITH course, collect(distinct(p)) as persons
WHERE size(persons) >= 2
RETURN collect(course) as courses