toshiro92
toshiro92

Reputation: 1344

Show nodes which have at least one relationship with specific labelled nodes

Hi !

I am currently trying to show via Neo4j Browser all nodes with a specific Label (Here named "Label1"), when each of them have at least one connection with 3 specific labelled nodes ("X","Y","Z"). I've tried the following query:

MATCH (a)-->(b)
where a:Label1
and size((a)-->(b)) >= 1
and b:X
and b:Y
and b:Z
RETURN a,b, size((a)-->(b)) AS count

However, it returns me everything instead of only nodes with these labels. Did I made something wrong here ?

I've also Tried to exclude nodes with labels I don't want to see, like:

and not b:LabelX

But they still appear.

Thank you :-)

Upvotes: 2

Views: 277

Answers (2)

InverseFalcon
InverseFalcon

Reputation: 30397

Since I'm assuming the :X, :Y, and :Z nodes are separate and not the same node, and that there are only one of each, you can get what you want with:

MATCH (a:Label1)
WHERE (a)-->(:X)
AND   (a)-->(:Y)
AND   (a)-->(:Z)
RETURN a

Upvotes: 1

The Demz
The Demz

Reputation: 7362

https://neo4j.com/docs/cypher-manual/current/syntax/patterns/#cypher-pattern-label

a must have label User

(a:User)-->(b)

a must have label User and Admin

(a:User:Admin)-->(b)

So,

MATCH (a)-->(b)
where a:Label1
and b:X
and b:Y
and b:Z
RETURN a, b

Could be written as:

MATCH (a:Label1)-->(b:X:Y:Z)
RETURN a, b

Upvotes: 0

Related Questions