How to update a node in Neo4j?

  • Read
  • Discuss

The SET clause is used in CQL to update a node by changing its properties. The SET clause is used to update labels, properties on nodes and relationships.

In this article, you’ll learn how to update a node by:

  • Setting a property
  • Updating a property
  • Removing a property 
  • Copying properties between nodes

Set a property

Using the SET clause, you can create a new property in a node or relationship.

Syntax

Following is the syntax for setting a property.

MATCH (node:label{properties . . . . . . . . . . . . . . })
SET node.property = value
RETURN node

Example:

Before adding a new property for a node, make sure you have the node like so:

CREATE (p3:person{name: "John", YOB: 1992, POB: "California"}) RETURN p3

Following is a sample Cypher Query to create a property named “Address” with value “H#123 st#xyz”.

MATCH (p3:person{name: "John", YOB: 1992, POB: "California"})
SET p3.Address="H#123 st#xyz"
RETURN p3

The newly-changed node is returned by the query.

Update a property

SET can be used to update a property on a node or relationship. 

Syntax

Following is the syntax of updating a property of a node using the SET clause.

MATCH (node:label {properties})
SET n.property =new property
RETURN node

Example

Following is a sample Cypher Query which updates the property named YOB from this node using the SET clause as shown below:

MATCH (p3:person{name: "John", YOB: 1992, POB: "California"})
SET p3.YOB=1993
RETURN p3

Remove a property

Although REMOVE is normally used to remove a property, it is sometimes convenient to do it using the SET command.You can remove an existing property by passing NULL as value to it.

Syntax

Following is the syntax of removing a property from a node using the SET clause.

MATCH (node:label {properties})
SET node.property = NULL
RETURN node 

Example

Following is a sample Cypher Query which removes the property named Address from this node using the SET clause as shown below.

MATCH (p3:person{name: "John", YOB: 1992, POB: "California"})
SET p3.Address=NULL
RETURN p3

The Address property is now missing.

Copy properties between nodes and relationships

SET can be used to copy all properties from one node or relationship to another using the properties() function. This will remove all other properties on the node or relationship being copied to.

Syntax

MATCH
  (node:label {property}),
  (node1:label {property})
SET node1 = properties(node)
RETURN node,node1

Example

Following is a sample Cypher Query which copies all properties from one node into another node using the SET clause as shown below:

MATCH
  (p3:person {name:"John"}),
  (p4:person)
SET p4 = properties(p3)
RETURN p3,p4

Leave a Reply

Leave a Reply

Scroll to Top