GraphGists

Create a Record for Yourself

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

CREATE creates nodes with labels and properties.

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.

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.

Find Your Friends

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

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)

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