SET clause
The SET
clause is used to update labels on nodes and properties on nodes and relationships.
- Setting a property
- Setting multiple properties
- Setting node labels
- Update a property
- Remove a property
- Copy all properties
- Replace all properties using map
- Update all properties using map
- Setting nested properties
- Removing nested properties
Dataset
The following examples are executed with this dataset. You can create this dataset locally by executing the queries at the end of the page: Dataset queries.
1. Setting a property
The SET
clause can be used to set the value of a property on a node or relationship:
MATCH (c:Country {name: 'Germany'})
SET c.population = 83000001
RETURN c.name, c.population;
Output:
+--------------+--------------+
| c.name | c.population |
+--------------+--------------+
| Germany | 83000001 |
+--------------+--------------+
2. Setting multiple properties
The SET
clause can be used to set the value of multiple properties nodes or relationships by separating them with a comma:
MATCH (c:Country {name: 'Germany'})
SET c.capital = 'Berlin', c.population = 83000002
RETURN c.name, c.population, c.capital;
Output:
+--------------+--------------+--------------+
| c.name | c.population | c.capital |
+--------------+--------------+--------------+
| Germany | 83000002 | Berlin |
+--------------+--------------+--------------+
3. Setting node labels
The SET
clause can be used to set the label on a node. If the node has a label, a new one will be added while the old one is left as is:
MATCH (c {name: 'Germany'})
SET c:Land
RETURN labels(c);
Output:
+---------------------+
| labels(c) |
+---------------------+
| ["Country", "Land"] |
+---------------------+
Multiple labels can be also set:
MATCH (c {name: 'Germany'})
SET c:Place:Area
RETURN labels(c);
Output:
+--------------------------------------+
| labels(c) |
+--------------------------------------+
| ["Country", "Land", "Place", "Area"] |
+--------------------------------------+
4. Update a property
The SET
clause can be used to update the value or type of a property on a node or relationship:
MATCH (c:Country {name: 'Germany'})
SET c.population = 'not available'
RETURN c.population;
Output:
+---------------+
| c.population |
+---------------+
| not available |
+---------------+
5. Remove a property
The SET
clause can be used to remove the value of a property on a node or relationship by setting it to NULL
:
MATCH (c:Country {name: 'Germany'})
SET c.population = NULL
RETURN c.population;
Output:
+--------------+
| c.population |
+--------------+
| Null |
+--------------+
6. Copy all properties
If SET
is used to copy the properties of one node/relationship to another, all the properties of the latter will be removed and replaced with the new ones:
MATCH (c1:Country {name: 'Germany'}), (c2:Country {name: 'France'})
SET c2 = c1
RETURN c2, c1;
Output:
+----------------------------------------------------------------------------+----------------------------------------------------------------------------+
| c2 | c1 |
+----------------------------------------------------------------------------+----------------------------------------------------------------------------+
| (:Country {continent: "Europe", language: "German", name: "Germany"}) | (:Country:Land {continent: "Europe", language: "German", name: "Germany"}) |
+----------------------------------------------------------------------------+----------------------------------------------------------------------------+
7. Replace all properties using map
If SET
is used with the property replacement operator =
, all the properties in the map that are on the node or relationship will be updated.
The properties that are not on the node or relationship but are in the map will be added. The properties that are not in the map will be removed.
MATCH (c:Country {name: 'Germany'})
SET c = {name: 'Germany', population: '85000000'}
RETURN c;
Output:
+------------------------------------------------------+
| c |
+------------------------------------------------------+
| (:Country {name: "Germany", population: "85000000"}) |
+------------------------------------------------------+
If an empty map is used, all the properties of a node or relationship will be set to NULL
:
MATCH (c:Country {name: 'Germany'})
SET c = {}
RETURN c;
Output:
+------------+
| c |
+------------+
| (:Country) |
+------------+
8. Update all properties using map
If SET
is used with the property mutation operator +=
, all the properties in the map that are on the node or relationship will be updated.
The properties that are not on the node or relationship but are in the map will be added. Properties that are not present in the map will be left as is.
MATCH (c:Country {name: 'Germany'})
SET c += {name: 'Germany', population: '85000000'}
RETURN c;
Output:
+-----------------------------------------------------------------------------------------------+
| c |
+-----------------------------------------------------------------------------------------------+
| (:Country {continent: "Europe", language: "German", name: "Germany", population: "85000000"}) |
+-----------------------------------------------------------------------------------------------+
9. Setting nested properties
Starting from version 3.6, Memgraph supports nested properties. Nested properties allow you to define and modify values inside Map
property types.
Before nested property support was introduced, users could only set base properties using queries such as:
MATCH (n:Person {name: 'Harry'}) SET n.age = 21;
With nested property support, you can now set properties inside a map, such as:
MATCH (n:Person {name: 'Harry'}) SET n.details.age = 21;
If the details
property does not already exist, Memgraph automatically creates it as a map and assigns the nested property within it.
This feature is especially useful when working with configuration objects or when optimizing graph storage, since maps typically consume less memory than multiple node or relationship objects.
You can query a nested property the same way you would any other:
MATCH (n:Person {name: 'Harry'})
RETURN n.details.age AS age;
Output:
```nocopy
+-----+
| age |
+-----+
| 21 |
+-----+
There are a few edge cases when working with nested properties:
If the parent property is not of type Map
, the query will throw an exception:
MATCH (n:Person {name: 'Harry'}) SET n.name.surname = 'Johnson' // ERROR because n.name is a string, not a map
{
Appending to nested properties
}You can also append to existing map properties using the +=
operator:
MATCH (n:Person {name: 'Harry'}) SET n.details += {age: 21};
When using this syntax:
- The right-hand side must be a
Map
. - The left-hand side must also be a
Map
(and must exist).
If either side is not a map, Memgraph will throw an exception. This ensures that map merging is always type-safe and consistent.
10. Removing nested properties
Starting from version v3.6, Memgraph also supports removing nested properties for easier manipulation of map objects within the node or relationship property store. The following query performs nested property removal:
MATCH (n:Person {name: 'Harry'}) REMOVE n.details.age;
This removes only the specified nested property (age
) while preserving all other keys in the parent map (details
).
If the property does not exist, Memgraph does not throw an exception - the behavior matches that of removing top-level properties.
Dataset queries
We encourage you to try out the examples by yourself. You can get our dataset locally by executing the following query block.
MATCH (n) DETACH DELETE n;
CREATE (c1:Country {name: 'Germany', language: 'German', continent: 'Europe', population: 83000000});
CREATE (c2:Country {name: 'France', language: 'French', continent: 'Europe', population: 67000000});
CREATE (c3:Country {name: 'United Kingdom', language: 'English', continent: 'Europe', population: 66000000});
MATCH (c1),(c2)
WHERE c1.name= 'Germany' AND c2.name = 'France'
CREATE (c2)<-[:WORKING_IN {date_of_start: 2014}]-(p:Person {name: 'John'})-[:LIVING_IN {date_of_start: 2014}]->(c1);
MATCH (c)
WHERE c.name= 'United Kingdom'
CREATE (c)<-[:WORKING_IN {date_of_start: 2014}]-(p:Person {name: 'Harry'})-[:LIVING_IN {date_of_start: 2013}]->(c);
MATCH (p1),(p2)
WHERE p1.name = 'John' AND p2.name = 'Harry'
CREATE (p1)-[:FRIENDS_WITH {date_of_start: 2011}]->(p2);
MATCH (p1),(p2)
WHERE p1.name = 'John' AND p2.name = 'Harry'
CREATE (p1)<-[:FRIENDS_WITH {date_of_start: 2012}]-(:Person {name: 'Anna'})-[:FRIENDS_WITH {date_of_start: 2014}]->(p2);
MATCH (p),(c1),(c2)
WHERE p.name = 'Anna' AND c1.name = 'United Kingdom' AND c2.name = 'Germany'
CREATE (c2)<-[:LIVING_IN {date_of_start: 2014}]-(p)-[:LIVING_IN {date_of_start: 2014}]->(c1);
MATCH (n)-[r]->(m) RETURN n,r,m;