cancel
Showing results forΒ
Did you mean:Β

## Cypher query

Node

I have many person nodes with a name property. I have one relationship "likes".

What is the query for "Who are the persons that like Ed but do not like (have no relationship with) Alice, Bob, Charlie, and Dan?"

1 ACCEPTED SOLUTION
Ninja

try this:

match(p:Person)-[:LIKES]->(:Person{name: βEdβ})

where not exist ((p)-[:LIKES]->(:Person{name: βAliceβ}))

and not exist ((p)-[:LIKES]->(:Person{name: βBobβ}))

and not exist ((p)-[:LIKES]->(:Person{name: βCharliβ}))

and not exist ((p)-[:LIKES]->(:Person{name: βDanβ}))

return p.name as person

Same query with a little fancier notation:

with [βAliceβ, βBobβ, βDanβ, βCharlieβ] as names

match(p:Person)-[:LIKES]->(:Person{name: βEdβ})

where none( x in names where exists ((p)-[:LIKES]->(:Person{name: x})))

return p.name as person

Ninja

try this:

match(p:Person)-[:LIKES]->(:Person{name: βEdβ})

where not exist ((p)-[:LIKES]->(:Person{name: βAliceβ}))

and not exist ((p)-[:LIKES]->(:Person{name: βBobβ}))

and not exist ((p)-[:LIKES]->(:Person{name: βCharliβ}))

and not exist ((p)-[:LIKES]->(:Person{name: βDanβ}))

return p.name as person

Same query with a little fancier notation:

with [βAliceβ, βBobβ, βDanβ, βCharlieβ] as names

match(p:Person)-[:LIKES]->(:Person{name: βEdβ})

where none( x in names where exists ((p)-[:LIKES]->(:Person{name: x})))

return p.name as person

Nodes 2022

NODES 2022, Neo4j Online Education Summit

OnΒ November 16 and 17 for 24 hours across all timezones, youβll learn about best practices for beginners and experts alike.

Neo4j Resources