How to find node with no incoming relationship in neo4j
Asked Answered
D

4

8

I am having 2 node types lets say of type 'Student' and 'Teacher'

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

Student can have optional relationship with Class node as 'TEACHES'.

(t:Teacher)-[r:TEACHES]->(c:Student).

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

I want to find "Student" nodes who don't have teacher. i.e There is no any incoming relationship "TEACHES"

Please help.

Deposition answered 21/4, 2014 at 11:4 Comment(0)
E
6

Here's a simple data setup, along with the query at the bottom you need to solve your problem. Essentially, you want to query for situations where a relationship doesn't exist. The syntax here is for neo4j 2.0, so the answer would be slightly different for older versions.

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
Edgar answered 21/4, 2014 at 12:13 Comment(2)
I wanted to find "Student" nodes who don't have teacher. Thanks for help.Deposition
Just run the query in the opposite direction: match (s:Student) where not (s)<-[:TEACHES]-() return s;Edgar
S
12

I think you're looking for this sort of pattern.

MATCH (s:Student)
WHERE NOT (s)<-[:TEACHES]-(:Teacher)
RETURN s
Savadove answered 21/4, 2014 at 13:3 Comment(0)
E
6

Here's a simple data setup, along with the query at the bottom you need to solve your problem. Essentially, you want to query for situations where a relationship doesn't exist. The syntax here is for neo4j 2.0, so the answer would be slightly different for older versions.

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
Edgar answered 21/4, 2014 at 12:13 Comment(2)
I wanted to find "Student" nodes who don't have teacher. Thanks for help.Deposition
Just run the query in the opposite direction: match (s:Student) where not (s)<-[:TEACHES]-() return s;Edgar
D
1

I got result by this. First match student criteria and then find relationship is null

MATCH (s:Student)
OPTIONAL MATCH (t:Teacher)-[r:TEACHES]->(s)
WITH s,r
WHERE r IS NULL
RETURN s.name
Deposition answered 21/4, 2014 at 12:50 Comment(1)
You could profile this query and compare to Wes', I think his is probably more straightforward (and I'd drop the :Teacher label unless you have non-teacher nodes that also -[:TEACH]-> student-nodes).Grindelia
A
0

If you are using Neo4j version 5+

This query will help you to find the all nodes which don't have any relationship.

match (n) where not (n)--() return n;

There is other option as well:

match(n)
optional match (n)-[r]-()
with id(n) as node, collect({id: id(r), type: type(r), startNode: id(startNode(r)), endNode: id(endNode(r))}) as rel
return {node: node, relationships: rel}

It will give you a list of each node that includes its relationships. Nodes without relationship will return as null for the node.

Arsenopyrite answered 27/10, 2023 at 4:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.