How to set or update objects
The set_()
method is used to set or update labels on nodes, and properties on
nodes and relationships.
set_(self, item: str, operator: Operator, **kwargs)
- sets or updates the value of item toliteral
orexpression
value, depending on theoperator
.
Set or update a property
You can assign a value to a node property with the query builder's set_()
method. The used assignment operator is imported from the query builder. You can
also use a simple equals sign as a string - "="
.
- GQLAlchemy
- Cypher
from gqlalchemy import Match
from gqlalchemy.query_builders.memgraph_query_builder import Operator
query = Match()
.node(labels="Country", variable="c", name="Germany")
.set_(item="c.population", operator=Operator.ASSIGNMENT, literal=83000001)
.return_()
.execute()
MATCH (c:Country {name: 'Germany'}) SET c.population = 83000001 RETURN *;
If the node already had the population
property, it will be updated by setting
it to a new value.
Set or update multiple properties
- GQLAlchemy
- Cypher
from gqlalchemy import Match
from gqlalchemy.query_builders.memgraph_query_builder import Operator
query = Match()
.node(variable='n')
.where(item='n.name', operator='=', literal='Germany')
.set_(item='n.population', operator=Operator.ASSIGNMENT, literal=83000001)
.set_(item='n.capital', operator=Operator.ASSIGNMENT, literal='Berlin')
.return_()
.execute()
MATCH (n) WHERE n.name = 'Germany' SET n.population = 83000001 SET n.capital = 'Berlin' RETURN *;
If the node already had the population
or capital
properties, they will be
updated to a new value.
Set a label
The set()
method can be used to set a label of a node. If a node already
has a label, then it will have both old and new label.
- GQLAlchemy
- Cypher
from gqlalchemy import Match
from gqlalchemy.query_builders.memgraph_query_builder import Operator
query = Match()
.node(variable="c", name="Germany")
.set_(item="c", operator=Operator.LABEL_FILTER, expression="Land")
.return_()
.execute()
MATCH (c {name: 'Germany'}) SET c:Land RETURN *;
Replace all properties using map
If set()
is used with the Operator.ASSIGNMENT
(=
), all the properties
in the map (value of the literal
argument) 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.
- GQLAlchemy
- Cypher
from gqlalchemy import Match
from gqlalchemy.query_builders.memgraph_query_builder import Operator
query = Match()
.node(variable='c', labels='Country')
.where(item='c.name', operator='=', literal='Germany')
.set_(item='c', operator=Operator.ASSIGNMENT, literal={'name': 'Germany', 'population': '85000000'})
.return_()
.execute()
MATCH (c:Country) WHERE c.name = 'Germany' SET c = {name: 'Germany', population: '85000000'} RETURN *;
Update all properties using map
If set()
is used with the Operator.INCREMENT
(+=
), all the properties
in the map (value of the literal
argument) 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.
- GQLAlchemy
- Cypher
from gqlalchemy import Match
from gqlalchemy.query_builders.memgraph_query_builder import Operator
query = Match()
.node(variable='c', labels='Country')
.where(item='c.name', operator='=', literal='Germany')
.set_(item='c', operator=Operator.INCREMENT, literal={'name': 'Germany', 'population': '85000000'})
.return_()
.execute()
MATCH (c:Country) WHERE c.name = 'Germany' SET c += {name: 'Germany', population: '85000000'} RETURN *;
Hopefully, this guide has taught you how to set or update node label or node and
relationship properties. For more information on what you can do with SET
clause, check out the Cypher manual. If you
have any more questions, join our community and ping us on
Discord.