Problem Statement

Let’s say we have the following data in Neo4j

We want to return a person node along with:

  • all the movies that person DIRECTED
  • all the props for each DIRECTED movie
  • SHOULD NOT return any movies that person ACTED_IN

Failed Solution (using Depth feature)

the simple solution is utilizing the depth feature

@Repository
public interface PersonRepository extends Neo4jRepository<Person, String> {

    Optional<Person> findByUuid(String uuid, @Depth int depth);
}

This query FAILS because it also returns movies the person ACTED_IN :[

Solution (without using Depth)

@Repository
public interface PersonRepository extends Neo4jRepository<Person, String> {

    @Query("MATCH (p:Person {uuid: $uuid})-[d:DIRECTED]->(m:Movie)-[h:HAS]->(prop:Prop)" +
            " return p, collect(d), collect(hh), collect(m), collect(prop)")
    Optional<Person> getPersonWithDirectedMovies(String uuid);
}