Neo4J ** **
##Querying the Graph
Finding the tagline of the movie title ‘The Matrix’ ~ MATCH (m:Movie) WHERE m.title = ‘The Matrix’ RETURN m.tagline ~
** Finding the person “Tom Hanks” and all the movie names that he has acted in? ** ~ MATCH (p:Person)-[:ACTED_IN]->(m:MOVIE) WHERE p.name = ‘TOM HANKS’ RETURN p, m ~
** Finding all movie names that has “Tom Hanks” acted in? ** ~ MATCH (p:Person {name: ‘TOM HANKS’})-[:ACTED_IN]->(m:MOVIE) RETURN m.title ~
Filtering Queries
Finding names of actors and their movie titles where the movie was released between 2000 and 2003? ~ MATCH (p:Person)-[:ACTED_IN]->(m:Movie) WHERE 2000 <= m.released <= 2003 RETURN p.name, m.title, m.released ~
Finding all movies that “Jack Nicholson” acted in and having movie’s tagline in the graph ~ MATCH (p:Person)-[:ACTED_IN]->(m:Movie) WHERE p.name = ‘JACK NICHOLSON’ AND m.tagline IS NOT NULL RETURN m.title, m.tagline ~
Finding all actors whose names begins with “Michael” ~ MATCH (p:Person)-[:ACTED_IN]->() WHERE p.name STARTS WITH ‘MICHAEL’ RETURN p.name
MATCH (p:Person)-[:ACTED_IN]->() WHERE toLower(p.name) STARTS WITH ‘MICHAEL’ RETURN p.name ~
Find all people who wrote the movie but did not direct the same movie ~ MATCH (p:Person)-[:WROTE]->(m:Movie) WHERE NOT exists( (p:)-[:DIRECTED]->(m) ) RETURN p.name, p.title ~
Finding all people born in 1965, 1970 and 1975 ~ MATCH (p:Person) WHERE p.born IN [1965, 1970, 1975] RETURN p.name, p.born ~
Finding names of all people who acted in ‘The Matrix’ who had the role of ‘Neo’ ~ MATCH (p:Person)-[r:ACTED_IN]->(m:Movie) WHERE ‘Neo’ IN r.roles AND m.title=’The Matrix’ RETURN p.name, r.roles ~
##Creating Nodes in a graph
Create a Person node with the name property value of ‘Tom Hanks’ ~ MERGE (p:Person{name:’Tom Hanks’}) RETURN p ~
##Creating relationships between nodes in a graph ~ MERGE (p:Person{name:’Tom Hanks’}) MERGE (m:Movie{title:’Apollo 13’}) MERGE (p)-[r:ACTED_IN]->(m) ~
~ MATCH (p:Person{name:’Michael Cain’}) MATCH (m:Movie{title:’The Dark Knight’}) MERGE (p)-[r:ACTED_IN]->(m) ~
~ MATCH (p:Person) WHERE p.name = ‘Daniel Kaluuya’ MERGE (m:Movie {title: ‘Get Out’}) MERGE (p)-[:ACTED_IN]->(m) RETURN p, m ~
##Updating properties in a graph
We use SET
to update the property of a node in a graph
~
MERGE (p:Person{name:’Tom Hanks’})
SET p.born = 1956
RETURN p
~
Adding property for movie ‘Apollo 13’ ~ MATCH (m:Movie{title:’Apollo 13’}) SET m.released = 1995 ~
Adding properties to a movie ‘Get Out’ ~ MATCH (m:Movie {title: ‘Get Out’}) SET m.tagline=’Gripping, scary, witty and timely!’ SET m.released=2017 RETURN m.title, m.tagline, m.released ~
We can also specify relationship property when we create the relationship as shown below: ~ MATCH (p:Person{name:’Tom Hanks’}) MATCH (m:Movie{title:’Apollo 13’}) MERGE (p)-[r:ACTED_IN{roles:[‘Jim LoveII’]}]->(m) ~
~ MATCH (p:Person{name:’Tom Hanks’}) MATCH (m:Movie{title:’Apollo 13’}) MERGE (p)-[r:ACTED_IN]}]->(m) SET r.roles = [‘Forrest’] ~
We can remove a role from a relationship as shown below: ~ MATCH (p:Person{name:’Tom Hanks’}) WHERE p.person == ‘Tom Hanks’ AND m.title=’Apollo 13’ REMOVE r.roles ~
Alternatively, we can also set r.roles
to null
~
MATCH (p:PERSON{name:’Tom Hanks’})
WHERE p.person == ‘Tom Hanks’ AND m.title=’Apollo 13’
SET r.roles = null
~
Merge Processing
Conditionally create or update nodes with the merge clause.
~ MERGE (p:Person{name:’Robbin Williams’}) ON MATCH SET p.died = 2014 RETURN p ~
~ MERGE (m:Movie{title:’Freaky’}) ON CREATE SET m.released = 2020 ON MATCH SET m.revenue = 15920855 RETURN m ~
Adding or updating a movie ~ MERGE (m:Movie {title: ‘Rocketman’}) // perform the ON MATCH setting of the matchedAt property ON MATCH SET m.matchedAt = datetime() // perform the ON CREATE setting of the createdAt property ON CREATE SET m.createdAt = datetime() // set the updatedAt property SET m.updatedAt = datetime() RETURN m ~
Deleting nodes and relationships
Deleting a relationship in a graph ~ MATCH (p:PERSON{name:’Tom Hanks’}) WHERE p.person == ‘Tom Hanks’ AND m.title=’Apollo 13’ DELETE r ~
Deleting a node in a graph if it doesn’t have a reference to other nodes ~ MATCH (m:Movie) WHERE m.title=’Apollo 13’ DELETE m ~
Deleting a node in a graph if it has a reference to other nodes ~ MATCH (m:Movie) WHERE m.title=’Apollo 13’ DETACH DELETE m ~