my-learning-notes

Cypher

Introduction

Cypher is the query language you use to retrieve data from the Neo4j Database.

After reading this article, you are expected to understand how to:

Data Model

The examples shown in this article will be using the data model shown in the image below.

Elements of A Cypher query

A simple cypher query will look like this:

MATCH (m:Movie {title: 'Cloud Atlas'})<-[:ACTED_IN]-(p:Person)
RETURN p.name

The query above will return the name of Person’s who acted in the Movie titled Cloud Atlas.

Clauses in Cypher

Clauses for reading data

In the cypher query sample above, you’ve seen the clause MATCH and RETURN. But what does those clauses do? The list below will explore the clauses in cypher and what does each keyword do.

Clauses for writing data

Create Node/Relationships

The clause to read data can also be chained with the clauses for creating node/relationships such as:

MATCH (p:Person)
WHERE p.name = "Daniel Kaluuya"
MERGE (m:Movie{title:"Get Out"})
MERGE (p)-[:ACTED_IN]->(m)

Updating properties

In the section above you’ve seen how to create a node property on creation. But what about updating a property of a node that already exists?

In order to do that cypher uses the SET clause.

MATCH (p:Person)-[r:ACTED_IN]->(m:Movie)
WHERE p.name = 'Michael Cain' AND m.title = 'The Dark Knight'
SET r.roles = ['Alfred Penny'], r.year = 2008
RETURN p, r, m

or

MATCH (m:Movie {title: 'Get Out'})
SET m.tagline = "Gripping, scary, witty and timely!", m.released = 2017
RETURN m.title, m.tagline, m.released

Removing Properties

There 2 ways to remove a property. First, is by using the REMOVE clause.

MATCH (p:Person)-[r:ACTED_IN]->(m:Movie)
WHERE p.name = 'Michael Cain' AND m.title = 'The Dark Knight'
REMOVE r.roles
RETURN p, r, m

Next, is by setting the property to null

MATCH (p:Person)
WHERE p.name = 'Gene Hackman'
SET p.born = null
RETURN p

Conditional Property Updating

Up to this point, you’ve seen how to create a node along with it’s properties and updating a node’s properties.

However, there are other cases where you want to create only certain node properties during node creation or update the node properties if it already exist. Enter, the ON CREATE SET and ON MATCH SET clauses. When a MERGE clause is followed by an ON CREATE SET clause, it will only create the properties defined after the ON CREATE SET. Following the whole cypher query with the ON MATCH SET will update the properties only when the node already exists in the database.

// Find or create a person with this name
MERGE (p:Person {name: 'McKenna Grace'})

// Only set the `createdAt` property if the node is created during this query
ON CREATE SET p.createdAt = datetime()

// Only set the `updatedAt` property if the node was created previously
ON MATCH SET p.updatedAt = datetime()

// Set the `born` property regardless
SET p.born = 2006

RETURN p

Deleting Node/Relationships

To delete nodes/relationships, there are 2 clauses.

Certification

Cypher Fundamentals

Next Reading

Graph Data Modeling