"I am playing around with Neo4J, and I have nodes of persons and cities visited. Lets say that I have visited London, and I want to see I want to create a query returning all cities that has been visited by more than one of the persons that have visited a city you have visited. However, it must not be a city that you have visited. Whoa, that was a mouthful."
We’ll figure this out in a couple of steps using the data provided:
create (Chris:person {name:'Chris'}),(Jeff:person {name:'Jeff'}),(Pete:person {name:'Pete'}),(Alex:person{name:'Alex'}),(John:person {name:'John'}),(London:city {name:'London'}),(Paris:city {name:'Paris'}),(Mumbay:city {name:'Mumbay'}),(Moskva:city {name:'Moskva'}),(Seattle:city {name:'Seattle'}),(Chris)-[:VISITED]->(London),(Jeff)-[:VISITED]->(London),(John)-[:VISITED]->(London),(Jeff)-[:VISITED]->(Paris),(John)-[:VISITED]->(Paris),(Jeff)-[:VISITED]->(Seattle),(London)-[:VISITED_BY]->(Chris),(London)-[:VISITED_BY]->(Jeff),(London)-[:VISITED_BY]->(John),(Paris)-[:VISITED_BY]->(Jeff),(Paris)-[:VISITED_BY]->(John),(Seattle)-[:VISITED_BY]->(Jeff),(Pete)-[:VISITED]->(Seattle),(Seattle)-[:VISITED_BY]->(Pete),(Chris)-[:VISITED]->(Mumbay),(Mumbay)-[:VISITED_BY]->(Chris),(Alex)-[:VISITED]->(Mumbay),(Mumbay)-[:VISITED_BY]->(Alex),(Alex)-[:VISITED]->(Moskva),(Moskva)-[:VISITED_BY]->(Alex),(Pete)-[:VISITED]->(Moskva),(Moskva)-[:VISITED_BY]->(Pete)-
John, Jeff both visited London
-
Alex visited Mumbay
MATCH (chris:person {name:'Chris'})-[:VISITED]->(city)<-[:VISITED]-(otherPerson)
RETURN otherPerson.name;-
London (visited by Jeff and John)
-
Paris (visited by Jeff and John)
-
Seattle (visited by Jeff)
-
Mumbay (visited by Alex)
-
Moskva (visited by Alex)
MATCH (chris:person {name:'Chris'})-[:VISITED]->(city)<-[:VISITED]-(otherPerson)
MATCH (otherPerson)-[:VISITED]->(otherCity)
RETURN distinct otherCity.name;Which cities were visited by people who visited the same cities that I’ve visited BUT that city should not have been visited by me AND more than one person should have visited that city.
-
Paris (Seattle had just Jeff visiting, Moskva just had Alex visiting. London & Mumbay have been visited by Chris)
MATCH (chris:person { name:'Chris' })-[:VISITED]->(city)<-[:VISITED]-(otherPerson)
MATCH (otherPerson)-[:VISITED]->(otherCity)
WHERE otherCity<>city
WITH otherCity.name AS cityName, collect(otherPerson.name) AS persons
WHERE length(persons)> 1
RETURN cityNameCreated by Luanne Misquitta, GraphAware