Neo4j Database – Part 04 – Cypher Object Notation

In the last blog I described the objects that make up a Neo4j database. In this blog I will discuss how we create each database object.

It would be impossible to learn Neo4j without learning the Cypher query language. At the time of writing the UI and the most popular client query libraries all rely on hand crafting Cypher code. I expect that future library updates will abstract some of this away however; in the mean time you need to get down and dirty with it. Make sure you have a link to chapter 3 of the developer manual for Cypher references. In addition to that reference, a cheatsheet is also maintained by Neo4j.

Quirky Syntax Notation

The first time you look at a Cypher query it just appears to be a jumble of brackets and symbols.

MATCH (n)-[r]-() RETURN COUNT(r)

So let’s just take a look at its format to understand the pieces. Note: Neo4j is case sensitive.

Node Notation

Parenthesis ( ) are the notation for one or more Nodes. We can create two nodes as follows:



We get two nodes with nothing in them except a unique internal ID that Neo4j uses. Pretty useless. We can fetch these two nodes as follows:


So the MATCH command is used to fetch nodes and as we will see later, it does it using patterns. In our case above it just says ‘fetch all nodes’.

The n is a variable which is passed down to other commands in the query and in this case it was passed to the RETURN command which says ‘return all the data for nodes that are in variable n‘.

Label Notation

Labels are used with Nodes. The notation for a label is the prefix : (a colon). We can create a node with one or more labels as follows:

CREATE (:Person)

CREATE (:Person:Family)

We can then fetch the two Person labelled nodes using:

MATCH (n:Person) RETURN n

Or just the one Family labelled node using:

MATCH (n:Family) RETURN n

To fetch both Person and Family nodes we need to add a WHERE (filter) clause.

MATCH (n) WHERE n:Person OR n:Family RETURN n

Notice when we did the CREATE above a variable n was not added. It is optional because there was no other clauses after the CREATE.

Property Notation

Properties are used with Nodes and Relationships and the format is the same for both. The notation for a property are braces { }. Nodes would be useless without properties and where appropriate, are added to relationships. We can create a node with properties as follows:

CREATE (:Person {name: “Fred”, age: 25, male: true})

We can get the age of Fred a couple of ways as follows:

MATCH (n:Person{name: “Fred”}) RETURN n

MATCH (n:Person) WHERE = “Fred” RETURN n

The two queries are internally identical however; it shows you the richness of the language. In the second query we used the variable n to filter on the property name.

Relationship Notation

Colon :, Square brackets [ ], hyphens and angle brackets < > are used in relationship notation. I will create two nodes using the same examples I used in the last blog.

CREATE (:Person {name:”Dad”})

CREATE (:Person {name:”Mum”})

We have our two nodes. Let’s create a MARRIED relationship between them. Like labels, the name is prefixed by a colon. Note: Most people put relationships in upper case. It is not mandatory but a standard that is typically followed.

MATCH (a:Person{name: “Dad”}),(b:Person{name: “Mum”}) CREATE (a)-[:MARRIED]->(b)

Let’s break this down. The MATCH is used to locate two nodes. The CREATE is used to create a relationship between those two nodes. The relationship piece is -[:MARRIED]->. It is always –[:<name>]-. I mentioned in the previous blog about relationship direction. We have to supply a direction and with this relationship it does not matter which way round it is. When creating the relationship you also add either a < at the beginning or a > at the end.

We can now answer the question “who is married to dad”

MATCH (a:Person)-[MARRIED]-(b:Person) WHERE = “Dad” RETURN b

Here you can really see the use of the variables a and b. Let’s add another relationship with a property as follows:

MATCH (a:Person{name: “Dad”}),(b:Person{name: “Mum”}) CREATE (a)-[:KNOWS {met: 20100101 }]->(b)

I created a KNOWS relationship with a property called met. I gave it a date in the format YYYYMMDD. (Neo4j has no built in date types or date handling functions which is quite strange. In fact in some data models the years, months and days are modelled as individual nodes.). Let query on this property.

MATCH n = ()-[r:KNOWS]-() WHERE r.met > 20000101 RETURN n

In this example variable n in this syntax refers to the path from a node through the relationship to the next node. The query returns every node on both sides of the KNOWS relationship and the r variable is used to access the met property on the relationship.

In this blog we just touched on using Cypher to explain the syntax. As the series progresses more Cypher will be explained. In the next blog I will discuss creating how we create thousands of nodes and relationships by importing data.









Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s