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:
MERGE (m:Movie {title: "Arthur the King"})
SET m.year = 2024
RETURN mRun this Cypher statement, that creates Movie and User nodes and a RATED relationship between them.
MERGE (m:Movie {title: "Arthur the King"})
MERGE (u:User {name: "Adam"})
MERGE (u)-[r:RATED {rating: 5}]->(m)
RETURN u, r, mThe 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:
MERGE (m:Movie {title: "Arthur the King"})
SET m.year = 2024
RETURN mYou should be able to identify that:
-
The
MERGEclause is used. -
The pattern creates a node with the label
Movie-(m:Movie) -
The
titleis included as part of the pattern -{title: "Arthur the King"} -
A single property
yearis set -SET m.year = 2024
Run the statement to create the new node.
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:
MERGE (m:Movie {title: "Arthur the King"})
MERGE (u:User {name: "Adam"})
MERGE (u)-[r:RATED {rating: 5}]->(m)
RETURN u, r, mThe statement creates:
-
A
MovienodeMERGE (m:Movie {title: "Arthur the King"}). -
A
UsernodeMERGE (u:User {name: "Adam"}). -
A
RATEDrelationship between them, that has aratingproperty of5MERGE (u)-[r:RATED {rating: 5}]→(m).
Run the statement to create the 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:
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
MERGE (m:Movie {title: "Movie title"})
MERGE (u:User {name: "User name"})
MERGE (u)-[r:RATED {rating: 1}]->(m)
RETURN u, r, mWhen you are ready continue to the next module.
Summary
In this lesson, you learned how to use the MERGE 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.