4.1. Create nodes and relationships

Create a node for the actor Tom Hanks:

CREATE (n:Actor { name:"Tom Hanks" });

Let’s find the node we created:

MATCH (actor:Actor)
WHERE actor.name = 'Tom Hanks'
RETURN actor;

Now let’s create a movie and connect it to the Tom Hanks node with an ACTED_IN relationship:

MATCH (actor:Actor)
WHERE actor.name = 'Tom Hanks'
CREATE (movie:Movie { title:'Sleepless IN Seattle' })
CREATE (actor)-[:ACTED_IN]->(movie);

This is how our graph looks now:

cypherdoc-created-first-movie-9149101a.svg

We can do more of the work in a single clause. CREATE UNIQUE will make sure we don’t create duplicate patterns. Using this: [r:ACTED_IN] lets us return the relationship.

MATCH (actor:Actor)
WHERE actor.name = 'Tom Hanks'
CREATE UNIQUE (actor)-[r:ACTED_IN]->(movie:Movie { title:"Forrest Gump" })
RETURN r;

Set a property on a node:

MATCH (actor:Actor)
WHERE actor.name = 'Tom Hanks'
SET actor.DoB = 1944
RETURN actor.name, actor.DoB;

The labels Actor and Movie help us organize the graph. Let’s list all Movie nodes:

MATCH (movie:Movie)
RETURN movie AS `All Movies`;
All Movies
2 rows

Node[2]{title:"Sleepless in Seattle"}

Node[3]{title:"Forrest Gump"}