Skip to main content

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 to literal or expression value, depending on the operator.

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 - "=".

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()

If the node already had the population property, it will be updated by setting it to a new value.

Set or update multiple properties

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()

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.

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()

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.

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()

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.

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()

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.