cancel
Showing results for 
Search instead for 
Did you mean: 

Cypher query

jpl
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

glilienfield
Ninja
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

View solution in original post

1 REPLY 1

glilienfield
Ninja
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
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.