Check your understanding
1. MERGE processing
If there are no nodes in the graph, what would this Cypher statement do?
MERGE (p:Person {name: 'Lucille Ball'})-[:ACTED_IN]->(m:Movie {title: 'Forever, Darling'})-
❏ It creates one relationship and no nodes.
-
❏ It creates two nodes and no relationship.
-
✓ It creates two nodes and one relationship.
-
❏ It does not modify the graph.
Hint
This code will try to find the Person node for Lucile Ball. It will also try to find the Movie node for Forever Darling. The graph has neither of these nodes in it. It will continue by completing the pattern.
Solution
It creates two nodes and one relationship.
This code will try to find the Person node for Lucile Ball. Since this graph has no Person node for Lucile Ball, it will create it. It will also try to find the Movie node for Forever Darling. It will not find it and create the Movie node. It will then create the relationship between the two newly-created nodes.
2. Update an existing node in the graph
Our graph has a Person node for Lucille Ball. Suppose we want to add the year that Lucille Ball was born to this node. The Person node for Lucille Ball has only the name property set to Lucille Ball.
How can we update this code to include her birth year of 1911?
Use the dropdown below complete the code.
MERGE (p:Person {name: 'Lucille Ball'})
/*select:ON MATCH*/
SET p.born = 1911
RETURN p-
❏
ON UPDATE -
❏
ON CREATE -
✓
ON MATCH -
❏
ON FOUND
Once you have selected your option, click the Check Results query button to continue.
Hint
Remember that MERGE first does a MATCH and if not found, it wll create the node.
Since the Lucille Ball node already exists, what condition do we expect when MERGE executes?
Solution
ON MATCH is the correct answer. We want the property set when the Lucille Ball node is found in the graph.
You use ON CREATE for behavior to execute if the node is created.
There are no ON UPDATE or ON FOUND clauses in Cypher.
Summary
In this lesson, you learned how you can perform additional setting of properties when you create or find a node in the graph. In the next challenge, you will demonstrate how to add or update properties when a node is created or found in the graph.