Amos
Amos

Reputation: 45

Neo4J Match nodes where all nodes with specific value

I have 2 nodes A and B, with a relationship between them. A and B have status property (0 or 1)

I want all nodes A with status = 0 and I want just nodes A that have B.status = 1 (but all b.status must equal 1, if one of them equal 0 I don't want node A)

I make this query

MATCH (a:A)-[r]-(b:B) WHERE a.status = 0 AND ALL(x IN b.status WHERE x = 1)
RETURN a.status, b.status

but I have A node with b.status = 0 ...

Upvotes: 1

Views: 629

Answers (1)

Christophe Willemsen
Christophe Willemsen

Reputation: 20185

Update after comment

You need to collect the B nodes in order to do the check with ALL

MATCH (a:A)-[r]-(b:B)
WHERE a.status = 0
WITH a, collect(b) as bNodes
WHERE ALL(x IN bNodes WHERE x.status = 1)
UNWIND bNodes as b
RETURN a.status, b.status

You don't need an ALL predicate here, just use AND :

MATCH (a:A)-[r]-(b:B)
WHERE a.status = 0
AND b.status = 1
RETURN a.status, b.status

Upvotes: 2

Related Questions