cancel
Showing results for 
Search instead for 
Did you mean: 

Match where end nodes share same property value

I would like to match a path where the end nodes share the same property value. Let me use Neo4J's classic movies dataset to illustrate my question

Suppose I want to find out scenarios where a director has a working relationship with two or more actors that share the same birth year. That is,

(1) the director had directed a movie where there was two or more actors that share the same birth year; or
(2) the director had directed multiple movies, where the different actors in each different movies share the same birth year.

I don't have any fixed particular director, movie or actors in mind. I've attempted the following cypher syntax:

However, my syntax looks very convoluted given that this is a simple query. In addition, I'm limited to just finding two actors with the same birth year with this syntax of mine. I also want to find cases where there are three, four or more actors with the same birth year having a working relationship with the director. Lastly, I'm not even confident whether or not this syntax of mine satisfies scenario (1), where the director directed a single movie and that single movie has multiple actors with the same birth year

Would appreciate advice on how I can improve on my search. Thank you!

1 ACCEPTED SOLUTION

In general I don't think you syntax is convoluted, just the pattern is expressed like that.

To generalize you can do also:

MATCH (p:Person)-[:ACTED_IN]->(:Movie)<-[:DIRECTED]-(d:Person)
WITH d, p.born as attribute, count(*) as count, collect(p) as people
WHERE count > 1
RETURN d, people, count

I.e. match one half of the pattern and then aggregate on the property, and select all results that exceed your overlap count.

View solution in original post

2 REPLIES 2

In general I don't think you syntax is convoluted, just the pattern is expressed like that.

To generalize you can do also:

MATCH (p:Person)-[:ACTED_IN]->(:Movie)<-[:DIRECTED]-(d:Person)
WITH d, p.born as attribute, count(*) as count, collect(p) as people
WHERE count > 1
RETURN d, people, count

I.e. match one half of the pattern and then aggregate on the property, and select all results that exceed your overlap count.

Thank you! Your suggestion is helpful in allowing me to see the output in a clearer manner indeed.

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.