user1879408
user1879408

Reputation: 2028

OPTIONAL MATCH and WHERE in Cypher

I'm struggling to write a cypher-query.

Graph

The picutre bellow shows the complete graph. Some movies do not have a stuntman (the graph is fictional).

Question

I wanna get ALL ACTORS (and THEIR MOVIES) who never played in a movie with a stuntman. In this case it would be "Johnny Depp"

Complete cypher graph

Upvotes: 4

Views: 1485

Answers (2)

Dave Bennett
Dave Bennett

Reputation: 11216

I think this will get you going

// Find the actors and their movies
MATCH (a:Actor)--(m:Movie)

// where the actor was never in a movie with a stuntman
WHERE NOT (a)-[:ACTS_IN]-(:Movie)-[:ACTS_IN]-(:Stuntman)
RETURN a,m

Upvotes: 2

logisima
logisima

Reputation: 7458

This should work :

MATCH (n:Actor)-->(m:Movie)
WHERE NOT (n)-->()<--(:Stuntman)
RETURN n AS actor, collect(m) AS movies

Cheers

PS: there is an other solution, but less performant I think :

MATCH (n:Actor)-->(m:Movie)
WITH n AS actor, collect(m) AS movies
WHERE all(m IN movies WHERE not (m)<--(:Stuntman))
RETURN actor, movies

Upvotes: 4

Related Questions