我有2个节点类型,可以说类型为“学生”和“老师”

Student have {id, name}.
Teacher have {id, name}.

学生可以与“类(class)”节点具有“TEACHES”的可选关系。
(t:Teacher)-[r:TEACHES]->(c:Student).

[r:TEACHES] - Optional relationship. (present or may not present)

我想找到没有老师的“学生”节点。即没有任何传入关系“TEACHES”

请帮忙。

最佳答案

这是一个简单的数据设置,下面是您需要解决问题的底部查询。本质上,您想查询不存在关系的情况。此处的语法适用于neo4j 2.0,因此答案与旧版本会略有不同。

neo4j-sh (?)$ create (t:Teacher {name:"Bob"})-[r:TEACHES]->(s:Student {name:"Mary"});
+-------------------+
| No data returned. |
+-------------------+
Nodes created: 2
Relationships created: 1
Properties set: 2
Labels added: 2
19 ms

neo4j-sh (?)$ create (t:Teacher {name:"Mark"});
+-------------------+
| No data returned. |
+-------------------+
Nodes created: 1
Properties set: 1
Labels added: 1
5 ms

neo4j-sh (?)$ MATCH (s:Student) WHERE NOT (s)<-[:TEACHES]-(:Teacher) RETURN s

10-04 13:58