Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
01-12-2023 05:38 AM
//Create test data
CREATE (parent1:PARENT{parent_no:1,name:"dad1"})
CREATE (parent2:PARENT{parent_no:2,name:"dad2"})
CREATE (child1:CHILD{name:"child1"})
MERGE (parent1)-[r:PARENT_OF]->(child1)
RETURN parent1, parent2, child1
1. THE APOC Query is as follows: This correctly modifies the child node because
MATCH (parent:PARENT{name:"dad1"})
WITH elementId(parent) as parent_id
MATCH(parent) WHERE elementId(parent)= parent_id
MATCH (parent)-[relation:PARENT_OF]->(child:CHILD)
CALL apoc.do.when(
child is NULL,
'//when child is null
MATCH(parent) WHERE elementId(parent)= parent_id
CREATE (new_child:CHILD{name:"child1"})
set new_child.created=true
MERGE (parent) -[r:PARENT_OF]-> (new_child)
RETURN new_child as child, parent as parent, "created" as action',
'//when child is null
SET child.modified =true
RETURN child as child, parent as parent, "modified" as action',
// variables in the block
{parent_id:parent_id, parent:parent, child:child}
)
YIELD value
RETURN value.child, value.parent, value.action
I correctly get the result of child1, parent1, "modified" and child 1 has modified attribute
2. THE APOC Query when modified for dad2 should create another child1 node but I get no result , no error , no changes or records
MATCH (parent:PARENT{name:"dad2"})
WITH elementId(parent) as parent_id
MATCH(parent) WHERE elementId(parent)= parent_id
MATCH (parent)-[relation:PARENT_OF]->(child:CHILD)
CALL apoc.do.when(
child is NULL,
'//when child is null
MATCH(parent) WHERE elementId(parent)= parent_id
CREATE (new_child:CHILD{name:"child1"})
set new_child.created=true
MERGE (parent) -[r:PARENT_OF]-> (new_child)
RETURN new_child as child, parent as parent, "created" as action',
'//when child is null
SET child.modified =true
RETURN child as child, parent as parent, "modified" as action',
// variables in the block
{parent_id:parent_id, parent:parent, child:child}
)
YIELD value
RETURN value.child, value.parent, value.action
Thanks for any help / guidance
Solved! Go to Solution.
01-12-2023 06:25 AM
Because no parent is returned in the query
MATCH (parent)-[relation:PARENT_OF]->(child:CHILD)
So if parent is null it cannot create anything. You need to first fetch parent, then with OPTIONAL keyword fetch child. This way parent will be returned even if it has no child. Below query works.
MATCH (parent:PARENT{name:"dad2"}) OPTIONAL MATCH(parent)-[relation:PARENT_OF]->(child:CHILD)
CALL apoc.do.when(
child is NULL,
'//when child is null
MERGE (parent)-[r:PARENT_OF]->(new_child:CHILD{name:"child1",created:true})
RETURN new_child as child, parent as parent, "created" as action',
'//when child is not null
SET child.modified =true
RETURN child as child, parent as parent, "modified" as action',
// variables in the block
{parent:parent, child:child}
)
YIELD value
RETURN value.child, value.parent, value.action
01-12-2023 06:25 AM
Because no parent is returned in the query
MATCH (parent)-[relation:PARENT_OF]->(child:CHILD)
So if parent is null it cannot create anything. You need to first fetch parent, then with OPTIONAL keyword fetch child. This way parent will be returned even if it has no child. Below query works.
MATCH (parent:PARENT{name:"dad2"}) OPTIONAL MATCH(parent)-[relation:PARENT_OF]->(child:CHILD)
CALL apoc.do.when(
child is NULL,
'//when child is null
MERGE (parent)-[r:PARENT_OF]->(new_child:CHILD{name:"child1",created:true})
RETURN new_child as child, parent as parent, "created" as action',
'//when child is not null
SET child.modified =true
RETURN child as child, parent as parent, "modified" as action',
// variables in the block
{parent:parent, child:child}
)
YIELD value
RETURN value.child, value.parent, value.action
All the sessions of the conference are now available online