Skip to content

Instantly share code, notes, and snippets.

@nawroth
Last active December 19, 2015 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save nawroth/5956880 to your computer and use it in GitHub Desktop.
Save nawroth/5956880 to your computer and use it in GitHub Desktop.

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

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`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment