Creating Graphs

You have used the MATCH clause to find patterns and read data from Neo4j.

In this lesson, you will learn how to create data.

The MERGE Clause

Creating Nodes and Relationships

Experiment by creating nodes and relationships in the database.

Run this Cypher statement, that uses MERGE to create a new Movie node:

cypher
MERGE (m:Movie {title: "Arthur the King"})
SET m.year = 2024
RETURN m

Run this Cypher statement, that creates Movie and User nodes and a RATED relationship between them.

cypher
MERGE (m:Movie {title: "Arthur the King"})
MERGE (u:User {name: "Adam"})
MERGE (u)-[r:RATED {rating: 5}]->(m)
RETURN u, r, m

The MERGE Clause

To create nodes and relationships in the database, you use the MERGE clause.

You can use the MERGE clause to create a pattern in the database. MERGE will only create the pattern if it doesn’t already exist.

Create a Movie node

Review this Cypher statement, that uses MERGE to create a new node:

cypher
MERGE (m:Movie {title: "Arthur the King"})
SET m.year = 2024
RETURN m

You should be able to identify that:

  • The MERGE clause is used.

  • The pattern creates a node with the label Movie - (m:Movie)

  • The title is included as part of the pattern - {title: "Arthur the King"}

  • A single property year is set - SET m.year = 2024

Run the statement to create the new node.

The new node that was just created

The new node is returned and you can view the set properties.

Create a RATED relationship

Relationships are created by expressing a pattern that connects two nodes.

Review this Cypher statement:

cypher
MERGE (m:Movie {title: "Arthur the King"})
MERGE (u:User {name: "Adam"})
MERGE (u)-[r:RATED {rating: 5}]->(m)
RETURN u, r, m

The statement creates:

  • A Movie node

    MERGE (m:Movie {title: "Arthur the King"}).

  • A User node

    MERGE (u:User {name: "Adam"}).

  • A RATED relationship between them, that has a rating property of 5

    MERGE (u)-[r:RATED {rating: 5}]→(m).

Run the statement to create the nodes and relationship.

The new nodes and relationship

Merging

MERGE uses the properties in the pattern to identify an existing node or relationship before creating it.

Running this query multiple times would only result in the nodes and relationship being created once. MERGE checks to see if the pattern exists before creating it.

Create your own movie

The database is out of date - the latest movie is Solace which was released in September 2016.

Search for the most recent movies

Run this query to return movies order by the most recent release date:

cypher
Latest Movies
MATCH (m:Movie)
WHERE m.released IS NOT NULL
RETURN m.title AS title, m.url AS url, m.released AS released
ORDER BY released DESC LIMIT 5
title url released

"Solace"

"https://themoviedb.org/movie/339527"

"2016-09-02"

"Mohenjo Daro"

"https://themoviedb.org/movie/402672"

"2016-08-12"

"Rustom"

"https://themoviedb.org/movie/392572"

"2016-08-12"

"Ben-hur"

"https://themoviedb.org/movie/271969"

"2016-08-12"

"Suicide Squad"

"https://themoviedb.org/movie/297761"

"2016-08-05"

Modify this query to add your favourite movie and a user rating

cypher
MERGE (m:Movie {title: "Movie title"})
MERGE (u:User {name: "User name"})
MERGE (u)-[r:RATED {rating: 1}]->(m)
RETURN u, r, m

When you are ready continue to the next module.

Summary

In this lesson, you learned how to use the CREATE clause to create nodes and relationships.

In the Cypher Fundamentals course you will learn more about reading and writing data to Neo4j.

In the next module you will learn how to get started with Neo4j and explore the Neo4j ecosystem.