我有两种节点类型可以说类型'学生'和老师'
Student have {id, name}.
Teacher have {id, name}.
学生可以与班级节点建立选择关系' TEACHES'。
(t:Teacher)-[r:TEACHES]->(c:Student).
[r:TEACHES] - Optional relationship. (present or may not present)
我想找到"学生"没有老师的节点。即没有任何传入关系" TEACHES"
请帮忙。
答案 0 :(得分:10)
我认为你正在寻找这种模式。
MATCH (s:Student)
WHERE NOT (s)<-[:TEACHES]-(:Teacher)
RETURN s
答案 1 :(得分:4)
这是一个简单的数据设置,以及您需要解决问题的底部查询。从本质上讲,您希望查询关系不存在的情况。这里的语法适用于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
答案 2 :(得分:1)
我得到了结果。首先匹配学生标准,然后找到关系为空
MATCH (s:Student)
OPTIONAL MATCH (t:Teacher)-[r:TEACHES]->(s)
WITH s,r
WHERE r IS NULL
RETURN s.name