The issue is that apoc.do.when is a method, not a function. As such you need a ‘call’ in front of the apoc method.
I am confused of what your query is trying to accomplish. As written, the query does nothing is no Person node exists, while it will always create a Person node with ‘name’ equal to ‘B’ if a Person node does exist. What outcome are you looking for?
Try this:
OPTIONAL MATCH (p:Person)
WITH COALESCE(p)as p1
CALL apoc.do.when(p1 is null,
'CREATE (a:Node{name:"A"}) RETURN a AS node',
'CREATE (b:Node{name:"B"}) RETURN b AS node',
{}
) as nodes
Return *
@glilienfield Thank you so, above code works fine, But in my case I want to create virtual node along with virtual relation between virtual node and original node based on multiple case statements
CALL apoc.do.case and create virtual relation from virtual node along with passing some property value from Person node. Like below
I want to create switch case and if case match create virtual node and virtual relation with source node as virtual node and target node as Matched node e.g p: Person, with one of the attribute of person node
match(p:Person)
CALL apoc.do.case([
true,
'
with apoc.create.vNode(["RightPerson"],{ key: p.value }) as vNode
return apoc.create.vRelationship(vNode, "VR", {}, $p ) as vRelationship, vNode
',
false,
'
with apoc.create.vNode(["WrongPerson"],{ key: p.value }) as vNode
return apoc.create.vRelationship(vNode, "VR", {}, $p ) as vRelationship, vNode
'],
'
with apoc.create.vNode(["WrongPerson"],{ key: p.value }) as vNode
return apoc.create.vRelationship(vNode, "VR", {}, $p ) as vRelationship, vNode
',
{p:p}
) yield value
return p as person, value.vNode as vNode, value.vRelationship as vRelationship
I assume you will replace the conditions 'true' and 'false' with real conditions. Also, the virtual relationships is the same for all three cases, so it could be removed from the 'do.case' statement and executed after the 'do.case' statement. I left it as is, in case you wanted to add different relationship properties or types.
I am trying to achieve based on of the attribute value from person node I want to create virtual node.
and returning original relationship along with create virtual relationship with virutalNode.
OPTIONAL MATCH (p:Person) where p.type='Doctor'
WITH p
CALL apoc.do.when(p.location=IN,
'CREATE virtualNode YIELD a AS node',
'CREATE virtualNode YIELD b AS node',
{}
) as nodes
Return *
Got it...try this. I didn't know what you wanted to set for the properties of the virtual nodes, so I added place holders. You can configure the maps to set all the virtual node properties you need. You can pass values through the apoc.do.when method's parameters map, which is empty right now, if you need properties from the 'p' node.
match (p:Person)
where p.type='Doctor'
call apoc.do.when(p.location='IN',
'call apoc.create.vNode(["Person"],{key: value}) YIELD node',
'call apoc.create.vNode(["Person"],{key: value}) YIELD node',
{}
) yield value
return p, value.node