Neo4j Notes

  7 mins read  

Neo4j Notes

CREATE (ee:Person { name: "Emil", from: "Sweden", klout: 99 })
MATCH (ee:Person) WHERE ee.name = "Emil" RETURN ee;

CREATE clause to create data

() parenthesis to indicate a node

ee:Person a variable ‘ee’ and label ‘Person’ for the new node

{} brackets to add properties to the node

MATCH clause to specify a pattern of nodes and relationships

(ee:Person) a single node pattern with label ‘Person’ which will assign matches to the variable ‘ee’

WHERE clause to constrain the results

ee.name = "Emil" compares name property to the value “Emil”

RETURN clause used to request particular results

MATCH (ee:Person) WHERE ee.name = "Emil" CREATE (js:Person { name: "Johan", from: "Sweden", learn: "surfing" }), (ir:Person { name: "Ian", from: "England", title: "author" }), (rvb:Person { name: "Rik", from: "Belgium", pet: "Orval" }), (ally:Person { name: "Allison", from: "California", hobby: "surfing" }), (ee)-[:KNOWS {since: 2001}]->(js),(ee)-[:KNOWS {rating: 5}]->(ir), (js)-[:KNOWS]->(ir),(js)-[:KNOWS]->(rvb), (ir)-[:KNOWS]->(js),(ir)-[:KNOWS]->(ally), (rvb)-[:KNOWS]->(ally)
MATCH (p:Person) RETURN p;

If you want to delete the nodes, you should delete the relationships that connected to them first.

MATCH (ee:Person)-[r:KNOWS]-()-[r1:KNOWS]-(p:Person) WHERE ee.name = "Johan" DELETE r,r1;

MATCH (ee:Person) DELETE ee;
MATCH (ee:Person)-[:KNOWS]-(friends) WHERE ee.name = "Emil" RETURN ee, friends

MATCH clause to describe the pattern from known Nodes to found Nodes

(ee) starts the pattern with a Person (qualified by WHERE)

-[:KNOWS]- matches “KNOWS” relationships (in either direction)

(friends) will be bound to Emil’s friends

MATCH (js:Person)-[:KNOWS]-()-[:KNOWS]-(surfer) WHERE js.name = "Johan" AND surfer.hobby = "surfing" RETURN DISTINCT surfer

Find the actor named “Tom Hanks”…

MATCH (tom {name: "Tom Hanks"}) RETURN tom

Find the movie with title “Cloud Atlas”…

MATCH (cloudAtlas {title: "Cloud Atlas"}) RETURN cloudAtlas

Find 10 people…

MATCH (people:Person) RETURN people.name LIMIT 10

Find movies released in the 1990s…

MATCH (nineties:Movie) WHERE nineties.released >= 1990 AND nineties.released < 2000 RETURN nineties.title

List all Tom Hanks movies…

MATCH (tom:Person {name: "Tom Hanks"})-[:ACTED_IN]->(tomHanksMovies) RETURN tom,tomHanksMovies

How people are related to “Cloud Atlas”…

MATCH (people:Person)-[relatedTo]-(:Movie {title: "Cloud Atlas"}) RETURN people.name, Type(relatedTo), relatedTo

Tom Hanks’ co-actors…

MATCH (tom:Person {name:"Tom Hanks"})-[:ACTED_IN]->(m)<-[:ACTED_IN]-(coActors) RETURN coActors.name

Who directed “Cloud Atlas”?

MATCH (cloudAtlas {title: "Cloud Atlas"})<-[:DIRECTED]-(directors) RETURN directors.name

Movies and actors up to 4 “hops” away from Kevin Bacon

MATCH (bacon:Person {name:"Kevin Bacon"})-[*1..4]-(hollywood) //长度最小1,最大4
RETURN DISTINCT hollywood

Bacon path, the shortest path of any relationships to Meg Ryan

MATCH p=shortestPath(
  (bacon:Person {name:"Kevin Bacon"})-[*]-(meg:Person {name:"Meg Ryan"})
)
RETURN p

Extend Tom Hanks co-actors, to find co-co-actors who haven’t work with Tom Hanks…

MATCH (tom:Person {name:"Tom Hanks"})-[:ACTED_IN]->(m)<-[:ACTED_IN]-(coActors),
      (coActors)-[:ACTED_IN]->(m2)<-[:ACTED_IN]-(cocoActors)
WHERE NOT (tom)-[:ACTED_IN]->()<-[:ACTED_IN]-(cocoActors) AND tom <> cocoActors
RETURN cocoActors.name AS Recommended, count(*) AS Strength ORDER BY Strength DESC

Find someone to introduce Tom Hanks to Tom Cruise

MATCH (tom:Person {name:"Tom Hanks"})-[:ACTED_IN]->(m)<-[:ACTED_IN]-(coActors),
      (coActors)-[:ACTED_IN]->(m2)<-[:ACTED_IN]-(cruise:Person {name:"Tom Cruise"})
RETURN tom, m, coActors, m2, cruise

MATCH (m:Movie)<-[:RATED]-(u:User) WHERE m.title CONTAINS "Matrix" WITH m.title AS movie, COUNT(*) AS reviews RETURN movie, reviews ORDER BY reviews DESC LIMIT 5;

Delete all Movie and Person nodes, and their relationships

MATCH (n) DETACH DELETE n

Note you only need to compare property values like this when first creating relationships Prove that the Movie Graph is gone

MATCH (n) RETURN n