5.1. Movie Database

Our example graph consists of movies with title and year and actors with a name. Actors have ACTS_IN relationships to movies, which represents the role they played. This relationship also has a role attribute.

We’ll go with three movies and three actors:

CREATE (matrix1:Movie { title : 'The Matrix', year : '1999-03-31' })
CREATE (matrix2:Movie { title : 'The Matrix Reloaded', year : '2003-05-07' })
CREATE (matrix3:Movie { title : 'The Matrix Revolutions', year : '2003-10-27' })
CREATE (keanu:Actor { name:'Keanu Reeves' })
CREATE (laurence:Actor { name:'Laurence Fishburne' })
CREATE (carrieanne:Actor { name:'Carrie-Anne Moss' })
CREATE (keanu)-[:ACTS_IN { role : 'Neo' }]->(matrix1)
CREATE (keanu)-[:ACTS_IN { role : 'Neo' }]->(matrix2)
CREATE (keanu)-[:ACTS_IN { role : 'Neo' }]->(matrix3)
CREATE (laurence)-[:ACTS_IN { role : 'Morpheus' }]->(matrix1)
CREATE (laurence)-[:ACTS_IN { role : 'Morpheus' }]->(matrix2)
CREATE (laurence)-[:ACTS_IN { role : 'Morpheus' }]->(matrix3)
CREATE (carrieanne)-[:ACTS_IN { role : 'Trinity' }]->(matrix1)
CREATE (carrieanne)-[:ACTS_IN { role : 'Trinity' }]->(matrix2)
CREATE (carrieanne)-[:ACTS_IN { role : 'Trinity' }]->(matrix3)

This gives us the following graph to play with:

Let’s check how many nodes we have now:

MATCH (n)
RETURN "Hello Graph with " + count(*)+ " Nodes!" AS welcome;

Return a single node, by name:

MATCH (movie:Movie { title: 'The Matrix' })
RETURN movie;

Return the title and date of the matrix node:

MATCH (movie:Movie { title: 'The Matrix' })
RETURN movie.title, movie.year;

Which results in:

movie.titlemovie.year
1 row

"The Matrix"

"1999-03-31"

Show all actors:

MATCH (actor:Actor)
RETURN actor;

Return just the name, and order them by name:

MATCH (actor:Actor)
RETURN actor.name
ORDER BY actor.name;

Count the actors:

MATCH (actor:Actor)
RETURN count(*);

Get only the actors whose names end with “s”:

MATCH (actor:Actor)
WHERE actor.name =~ ".*s$"
RETURN actor.name;

Here’s some exploratory queries for unknown datasets. Don’t do this on live production databases!

Count nodes:

MATCH (n)
RETURN count(*);

Count relationship types:

MATCH (n)-[r]->()
RETURN type(r), count(*);
type(r)count(*)
1 row

"ACTS_IN"

9

List all nodes and their relationships:

MATCH (n)-[r]->(m)
RETURN n AS FROM , r AS `->`, m AS to;
from->to
9 rows

Node[3]{name:"Keanu Reeves"}

:ACTS_IN[2]{role:"Neo"}

Node[2]{year:"2003-10-27",title:"The Matrix Revolutions"}

Node[3]{name:"Keanu Reeves"}

:ACTS_IN[1]{role:"Neo"}

Node[1]{year:"2003-05-07",title:"The Matrix Reloaded"}

Node[3]{name:"Keanu Reeves"}

:ACTS_IN[0]{role:"Neo"}

Node[0]{title:"The Matrix",year:"1999-03-31"}

Node[4]{name:"Laurence Fishburne"}

:ACTS_IN[5]{role:"Morpheus"}

Node[2]{year:"2003-10-27",title:"The Matrix Revolutions"}

Node[4]{name:"Laurence Fishburne"}

:ACTS_IN[4]{role:"Morpheus"}

Node[1]{year:"2003-05-07",title:"The Matrix Reloaded"}

Node[4]{name:"Laurence Fishburne"}

:ACTS_IN[3]{role:"Morpheus"}

Node[0]{title:"The Matrix",year:"1999-03-31"}

Node[5]{name:"Carrie-Anne Moss"}

:ACTS_IN[8]{role:"Trinity"}

Node[2]{year:"2003-10-27",title:"The Matrix Revolutions"}

Node[5]{name:"Carrie-Anne Moss"}

:ACTS_IN[7]{role:"Trinity"}

Node[1]{year:"2003-05-07",title:"The Matrix Reloaded"}

Node[5]{name:"Carrie-Anne Moss"}

:ACTS_IN[6]{role:"Trinity"}

Node[0]{title:"The Matrix",year:"1999-03-31"}