Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
12-14-2022 10:33 AM
I have written below query
Unknown function 'apoc.do.when'Can any one help me here, I have installed apoc plugin and when try to run separately its working
Solved! Go to Solution.
12-15-2022 09:32 AM
Try this:
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.
12-14-2022 10:41 AM - edited 12-14-2022 10:47 AM
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 *
12-14-2022 10:44 AM
Getting below exception
Invalid input 'apoc': expected
12-14-2022 12:03 PM
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?
12-14-2022 05:43 PM
@glilienfield Thanks for the reply.
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 *
12-15-2022 07:23 AM
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
12-15-2022 08:41 AM - edited 12-15-2022 08:45 AM
@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
12-15-2022 09:32 AM
Try this:
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.
All the sessions of the conference are now available online