Neo4j: Find set of nodes that all nodes point to -
to more precise, suppose have people , animal nodes, , :likes
relationship person
animal
. question is: how can find animal
s all people like?
example:
(person {name: "jake"})-[:likes]->(animal {name: "dog"}) (person {name: "maya"})-[:likes]->(animal {name: "dog"}) (person {name: "maya"})-[:likes]->(animal {name: "snake"}) (person {name: "jake"})-[:likes]->(animal {name: "cat"})
if jake , maya universe of people, set of animals both jake , maya contains dogs.
with example dataset:
create (jake:person {name:'jake'}), (maya:person {name:'maya'}), (dog:animal {name:'dog'}), (snake:animal {name:'snake'}), (cat:animal {name:'cat'}), (jake)-[:likes]->(dog), (jake)-[:likes]->(cat), (maya)-[:likes]->(dog), (maya)-[:likes]->(snake)
i think following reads nicely:
match (p:person) match (a:animal) a, collect(p) people all(p in people (p)-[:likes]->(a)) return a.name
http://console.neo4j.org/r/vu3vxp
but it's not fast as:
match (p:person) count(p) people match (p:person)-[:likes]->(a:animal) a, count(p) likes, people likes = people return a.name