Raj
Raj

Reputation: 31

How to get the count of excluded nodes in neo4j

I have nodes like this. enter image description here

I want to get all the nodes(from c1 to c5) which are not mapped to node (l1), so my result nodes should be (c2),(c4) and (c5). How can query in Neo4j.

Upvotes: 0

Views: 45

Answers (2)

František Hartman
František Hartman

Reputation: 15086

A naive solution would be (C, L are node labels R1 is relationship type)

MATCH (c:C)
WHERE NOT((c)-[:R1]->(l:L {...})
RETURN c

where (l:L {...}) is some filter to identify node l1.

If there is a high number of relationships coming out of C nodes that would become slow. You could try following:

MATCH (l:L {...})<-[:R1]-(c:C)
WITH COLLECT(c) AS cs
MATCH (c2:C)
WHERE NOT c2 NOT cs
RETURN c2

You should choose between the two based on your data.

Upvotes: 0

stdob--
stdob--

Reputation: 29167

You need to choose those nodes from which there is no one-way path to the desired node:

MATCH (E {name: 'l1'}) WITH E
MATCH (M) WHERE NOT (M)-[*]->(E)
RETURN M

Upvotes: 0

Related Questions