New York
Join 1000+ graph enthusiasts & innovators October 23–24  •  Use code WEB10 for 10% off!

GraphGist: First Steps with Cypher

by Michael Hunger

Create a Record for Yourself

CREATE (you:Person {name:"You"})
RETURN you

CREATE creates nodes with labels and properties.

Loading graph...

You like Neo4j, right?

Let’s find ourselves and add a new relationship to a new node.

MATCH  (you:Person {name:"You"})
CREATE (you)-[like:LIKE]->(neo:Database {name:"Neo4j" })
RETURN you,like,neo

CREATE can create single nodes, or more complex structures.

Loading graph...

Create Your Friends

MATCH (you:Person {name:"You"})
FOREACH (name in ["Johan","Rajesh","Anna","Julia","Andrew"] |
  CREATE (you)-[:FRIEND]->(:Person {name:name}))

FOREACH allows you to execute update operations for each element of a list.

Loading graph...

Find Your Friends

MATCH (you {name:"You"})-[:FRIEND]->(yourFriends)
RETURN you, yourFriends
Loading graph...
Loading table...

Note that we get ourselves repeated for each path found in the graph.

Create Second Degree Friends and Expertise

MATCH (neo:Database {name:"Neo4j"})
MATCH (anna:Person {name:"Anna"})
CREATE (anna)-[:FRIEND]->(:Person:Expert {name:"Amanda"})-[:WORKED_WITH]->(neo)
Loading graph...

CREATE can also add more complex patterns.

Find Someone in your Network Who Can Help You Learn Neo4j

MATCH (you {name:"You"})
MATCH (expert)-[:WORKED_WITH]->(db:Database {name:"Neo4j"})
MATCH path = shortestPath( (you)-[:FRIEND*..5]-(expert) )
RETURN db,expert,path
Loading graph...
Loading table...
Run
Table
Graph
Table!
Graph!
Error!
Loading