Neo4j - NOT IN query

20,073

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
Share:
20,073
Jack
Author by

Jack

Working as a software developer in BI/Analytics domain.

Updated on November 03, 2020

Comments

  • Jack
    Jack over 3 years

    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?

  • Jack
    Jack over 10 years
    NOT IN did not work for me. however using <> gave me the desired result
  • Gondil
    Gondil about 9 years
    I think WHERE NOT actor IN coactors is right. Am I wrong? Your version doesn't work Michael
  • Merrick
    Merrick about 7 years
    WHERE NOT thing IN list worked for me too. Thanks @Gondil
  • Benjamin R
    Benjamin R over 2 years
    @michael-hunger WHERE NOT thing IN list works correct, but it's awful syntax. Why doesn't cypher not support WHERE thing NOT IN list, also?