Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
12-13-2019 01:33 AM
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?
Solved! Go to Solution.
12-13-2019 03:04 AM
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...
12-13-2019 02:14 AM
MATCH (n { EmpId: '1234', DepartmentId: '100' })
SET n: Accountant
RETURN n.name , n.EmpId , n.DepartmentId
12-13-2019 02:59 AM
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()"
12-13-2019 03:04 AM
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...
12-13-2019 03:26 AM
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.
12-14-2019 02:15 PM
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.
12-16-2019 06:55 PM
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.
12-18-2019 05:24 AM
Thank you all for your updates. This worked
All the sessions of the conference are now available online