Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j - NOT IN query

The graph schema I have is (actors)-[:ACTED_IN]->(movies).

I know how to find actors who have worked with a particular actor as below:

MATCH (actor {name:"Tom Hanks"} )-[:ACTED_IN]->(movies)<-[:ACTED_IN]-(costars) return distinct costars;

I know how to find all the actors who have worked in some movie: MATCH (all_actor)-[:ACTED_IN]->(movies) return distinct all_actor;

However I don't know how to find all actors not in costars. How do I go about it?

like image 811
Jack Avatar asked Jan 17 '14 11:01

Jack


People also ask

What is N in Neo4j?

Copy to Clipboard Run in Neo4j Browser. MATCH (n) WHERE isEmpty(n.eyes) RETURN n.age AS age. The age are returned for each node that has a property eyes where the value evaulates to be empty (empty string).

What is unwind in Neo4j?

With UNWIND , you can transform any list back into individual rows. These lists can be parameters that were passed in, previously collect -ed result or other list expressions. One common usage of unwind is to create distinct lists. Another is to create data from parameter lists that are provided to the query.


1 Answers

As you want to deduct the coactors from the global list of actors this is not the best graph query, here are some suggestions.

// Max de Marzi
MATCH (actor:Actor {name:"Tom Hanks"})-[:ACTED_IN]->(movie), (other:Actor)
WHERE NOT (movie)<-[:ACTED_IN]-(other)
RETURN other

// Wes Freeman
MATCH (actor:Actor {name:"Tom Hanks"}), (other:Actor)
WHERE NOT (actor)-[:ACTED_IN]->()<-[:ACTED_IN]-(other)
RETURN other


// Michael Hunger
MATCH (actor:Actor {name:"Tom Hanks"} )-[:ACTED_IN]->(movies)<-[:ACTED_IN]-(coactor)
WITH collect(distinct coactor) as coactors
MATCH (actor:Actor)
WHERE NOT actor IN coactors
RETURN actor
like image 98
Michael Hunger Avatar answered Sep 23 '22 02:09

Michael Hunger