cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to set the Label for a node in a Case Statement

Kailash
Graph Buddy

Hi Team,

i can set the label for a node like this below

MATCH (n { EmpId: '1234' })
SET n: Accountant
RETURN n.name , n.EmpId , n.DepartmentId

but i want to set the label as Accountant only if the department id is matching to some value.
MATCH (n { EmpId: '1234' })
SET n: (CASE when DepartmentId='100' then Accountant ELSE END)
RETURN n.name , n.EmpId , n.DepartmentId

I can set some property as true and false in and then in next query i can set it only if its true but i don't want to create that way. Need in one query like above. Could this be done?

1 ACCEPTED SOLUTION

Try something like this:

FOREACH (_ IN CASE WHEN NOT csvReader.DepartmentId = csvReader.ParentId THEN [true] ELSE [] END |
put the update here
)

A great post with many tips and tricks for importing: https://medium.com/neo4j/5-tips-tricks-for-fast-batched-updates-of-graph-structures-with-neo4j-and-c...

View solution in original post

7 REPLIES 7

MATCH (n { EmpId: '1234', DepartmentId: '100' })
SET n: Accountant
RETURN n.name , n.EmpId , n.DepartmentId

Thanks Thomas for your response!
I tried to give this example and it works fine.
But I am loading the data from a CSV and here i need to set a label as Accountant only if the
csvReader.DepartmentId <> csvReader.ParentId . I tried below but it gives error. Looking for something like this. Please ignore the syntax error

*"USING PERIODIC COMMIT 1000 LOAD CSV WITH HEADERS FROM "file:///"myfile.csv AS csvReader FIELDTERMINATOR '|' *
WITH csvReader WHERE NOT csvReader.EmpId IS NULL
MERGE (emp:Employee{Id:toString(csvReader.EmpId)})
*ON CREATE SET emp.EmpId=csvReader.EmpId ,emp.ParentId=csvReader.ParentId, emp.DepartmentId=csvReader.DepartmentId,emp.Name=toLower(csvReader.Name),emp.LastName=csvReader.LastName, *
emp:(CASE when csvReader.DepartmentId <> csvReader.ParentId then 'Accountant' ELSE END) , emp.EmpUpdated=timestamp(), emp.EmpCreated=timestamp()

*ON MATCH SET emp.EmpId=csvReader.EmpId , emp.ParentId=csvReader.ParentId, emp.DepartmentId=csvReader.DepartmentId,emp.Name=toLower(csvReader.Name),emp.LastName=csvReader.LastName, *
emp:(CASE when csvReader.DepartmentId <> csvReader.ParentId then 'Accountant' ELSE END) , emp.EmpCreated=timestamp()"

Try something like this:

FOREACH (_ IN CASE WHEN NOT csvReader.DepartmentId = csvReader.ParentId THEN [true] ELSE [] END |
put the update here
)

A great post with many tips and tricks for importing: https://medium.com/neo4j/5-tips-tricks-for-fast-batched-updates-of-graph-structures-with-neo4j-and-c...

I would try something like this:

USING PERIODIC COMMIT 1000 LOAD CSV WITH HEADERS FROM "file:///"myfile.csv AS csvReader FIELDTERMINATOR '|' *
WITH csvReader WHERE NOT csvReader.EmpId IS NULL
MERGE (emp:Employee{Id:toString(csvReader.EmpId)})
*ON CREATE SET emp.EmpId=csvReader.EmpId ,emp.ParentId=csvReader.ParentId, emp.DepartmentId=csvReader.DepartmentId,emp.Name=toLower(csvReader.Name),emp.LastName=csvReader.LastName,emp.EmpCreated=timestamp(), emp.EmpUpdated=timestamp() *

*ON MATCH SET emp.EmpId=csvReader.EmpId , emp.ParentId=csvReader.ParentId, emp.DepartmentId=csvReader.DepartmentId,emp.Name=toLower(csvReader.Name),emp.LastName=csvReader.LastName,emp.EmpUpdated=timestamp() *

WITH emp WHERE emp.DepartmentId <> emp.ParentId 
SET emp:Accountant;

I haven't tested it, but it should work.

ameyasoft
Graph Maven

MATCH (n { EmpId: '1234' })
WHERE n.DepartmentID = '100'
SET n.Accountant
RETURN n.name , n.EmpId , n.DepartmentId

Here you are creating a property with no value assigned.

As @Thomas_Silkjaer mentioned, the FOREACH construct can be used to implement the optional portion of the label setting. This cypher implements the optional setting of the label to accountant only when the DepartmentId is not equal to the ParentId:

MERGE(n { EmpId: event.EmpId})
  ON CREATE 
	SET n.DepartmentId=event.DepartmentId,
	    n.ParentId = event.ParentId
  ON MATCH 
	SET n.DepartmentId=event.DepartmentId,
	    n.ParentId = event.ParentId
FOREACH (_ IN CASE WHEN NOT event.DepartmentId = event.ParentId THEN [true] ELSE [] END |
	SET n:Accountant
)	
RETURN n.EmpId , n.DepartmentId

This can be seen with two example sets of test data, the first where the values are equal:

{
	"EmpId":"match",
	"DepartmentId":"100",
	"ParentId":"100"
}

The second where the values are different:

{
	"EmpId":"nomatch",
	"DepartmentId":"200",
	"ParentId":"300"
}

I have tested this, and with this cypher, when the values are equal, no label is set. When they are not equal, the Accountant label is set.

Kailash
Graph Buddy

Thank you all for your updates. This worked