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 { 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);

Using a WHERE clause in the query above to get the Tom Hanks node does the same thing as the pattern in the MATCH clause of the previous query.

This is how our graph looks now:

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 { name: "Tom Hanks" })
CREATE UNIQUE (actor)-[r:ACTED_IN]->(movie:Movie { title:"Forrest Gump" })

Set a property on a node:

MATCH (actor: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[1]{title:"Sleepless in Seattle"}

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