Updating properties
Thus far, you have learned how to create nodes with MERGE where you specify the primary key property for the node.
You can add, modify, or remove properties from nodes and relationships.
In this lesson you will learn how to write Cypher code to update properties of nodes and relationships.
Adding properties for a node or relationship
There are two ways that you can set a property for a node or relationship.
1. Inline as part of the MERGE clause
You have already seen how to create the primary key property for a node. You can also set a property for a relationship inline as follows:
MATCH (p:Person {name: 'Michael Caine'})
MERGE (m:Movie {title: 'Batman Begins'})
MERGE (p)-[:ACTED_IN {roles: ['Alfred Penny']}]->(m)
RETURN p,mIn this code, the actor, Michael Caine exists but the movie, Batman Begins does not. We find the Person node and we create the Movie node.
Then, we create the ACTED_IN relationship between the Michael Caine node and the newly-created Batman Begins node. And we set the roles property for this relationship to an array of values - containing one value, Alfred Penny.
Notice that for inline property setting, we use the JSON-style of adding the property key/value pairs in braces { .. }, just like we did when we specified the property for the node.
2. Using the SET keyword for a reference to a node or relationship
We also have the option to use the SET keyword for setting a property value.
In the context of particular MERGE or MATCH clause where you have defined a variable to reference the node or relationship, you can set property values.
MATCH (p:Person)-[r:ACTED_IN]->(m:Movie)
WHERE p.name = 'Michael Caine' AND m.title = 'The Dark Knight'
SET r.roles = ['Alfred Penny']
RETURN p, r, mSetting multiple properties
If you need to set multiple properties, you separate them with a comma (,). For example:
MATCH (p:Person)-[r:ACTED_IN]->(m:Movie)
WHERE p.name = 'Michael Caine' AND m.title = 'The Dark Knight'
SET r.roles = ['Alfred Penny'], m.released = 2008
RETURN p, r, mUpdating properties
If you have a reference to a node or relationship, you can also use SET to modify the property.
For example, if we wanted to modify Michael Caine’s role to be something different, we could do the following:
MATCH (p:Person)-[r:ACTED_IN]->(m:Movie)
WHERE p.name = 'Michael Caine' AND m.title = 'The Dark Knight'
SET r.roles = ['Mr. Alfred Penny']
RETURN p, r, mRemoving properties
You can remove or delete a property from a node or relationship by using the REMOVE keyword, or setting the property to null.
Here we remove the roles property of this relationship:
MATCH (p:Person)-[r:ACTED_IN]->(m:Movie)
WHERE p.name = 'Michael Caine' AND m.title = 'The Dark Knight'
REMOVE r.roles
RETURN p, r, mHere we remove the born property from an actor:
MATCH (p:Person)
WHERE p.name = 'Gene Hackman'
SET p.born = null
RETURN pCheck your understanding
1. Creating, adding, removing properties
Suppose you have a reference to a node. What keyword do you use to create, update, or remove a property of the referenced node?
-
❏
INSERT -
✓
SET -
❏
UPDATE PROPERTY -
❏
UPDATE
Hint
This clause can be used to remove a property by setting it to null.
Solution
You use the SET clause to set a value to null in the graph.
2. Creating properties
What are the ways that you can add a property to a node or relationship?
-
✓ When you create it using
MERGEwhere you specify the property in braces {..}. -
❏ Create an index for it by its property key.
-
❏ Use the
ADD PROPERTYclause if you have a reference to the node or relationship. -
✓ Use the
SETclause if you have a reference to the node or relationship.
Hint
There are two ways to create properties, inline or setting them.
Solution
There are two ways to set property values in Cypher:
-
Specify the property values in the {..} when you create the node or relationship.
-
Use the
SETclause to set values to a reference to the node or relationship.
You do not create an index to set property values.
There is no ADD PROPERTY clause in Cypher.
3. Removing properties
Suppose we want to remove all tagline properties from all Movie nodes in the graph.
Use the dropdown below to complete the code.
MATCH (m:Movie)
/*select:REMOVE m.tagline*/
RETURN m-
❏
REMOVE tagline -
❏
DELETE m.tagline -
✓
REMOVE m.tagline -
❏
DELETE tagline
Once you have selected your option, click the Check Results query button to continue.
Hint
This MATCH clause will select all Movie nodes in the graph and reference it with the variable m.
What clause do you use the remove the property for every node retrieved.
Solution
REMOVE m.tagline is the correct answer. We have a reference to the Movie node so we can use the REMOVE clause to remove the property.
REMOVE tagline is incorrect, because we do not have a reference to the node.
There is no DELETE clause in Cypher.
Summary
In this lesson, you learned how to create, update and delete properties for nodes and relationships. In the next challenge, you will add properties to a node.