Pages

October 01, 2019

#Neo4j Part 4:Interview Questions & Answers (Set Clause)

What does the SET clause is used for in Neo4j?
Neo4j CQL use SET clause for the following purpose
  • Update or Add properties values
  • Add new properties to existing Relationship or Node

Setting a Property: We can create a new property in a node by using the SET clause.

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

Let's create a node:

CREATE(Employee:Developer{name:"seba",id:2})
RETURN Employee;

Now lets add another property 'age' in it.

MATCH(Employee:Developer{name:"seba",id:2})
SET Employee.age=27
RETURN Employee;
Removing a PropertyTo remove an existing property we need to pass value as NULL.

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

e.g Let's remove the property 'age' which we added in for an Employee with name 'seba'.

MATCH(Employee:Developer{name:"seba",id:2})
SET Employee.age=NULL
RETURN Employee;

Setting Multiple Properties: We can create multiple properties in a node using the Set clause, for this we need to specify these key value pairs with commas.

MATCH (node:label {properties}) 
SET node.property1 = value, node.property2 = value 
RETURN node

e.g:
MATCH(Employee:Developer{name:"seba",id:2})
SET Employee.age=NULL, Employee.maritalstatus=single
RETURN Employee;

Setting a Label on a Node: We can also set a label to an existing node using the SET clause.

MATCH (n {properties . . . . . . . }) 
SET n :label 
RETURN n 

e.g:
MATCH(Employee:Developer{name:"seba",id:2})
SET Employee:Lead
RETURN Employee;

Setting Multiple Labels on a Node: We can set multiple labels to an existing node using the SET clause, for this we specify the labels by separating them with colons “:”.

MATCH (n {properties . . . . . . . }) 
SET n :label1:label2 
RETURN n

e.g:
MATCH(Employee:Developer{name:"seba",id:2})
SET Employee:Lead:Head:Tester
RETURN Employee;

-K Himaanshu Shuklaa..

No comments:

Post a Comment