Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
11-17-2021 02:57 PM
How do I do the following in one cypher query?
I tried various versions of the code below but only the clone is created; the label is not renamed.
The code below is for one node with id = 'abc' to validate the procedures.
MATCH (n:OldLabel {id:'abc'})
WITH collect(n) as nodes
CALL apoc.refactor.cloneNodes(nodes,True) YIELD output as c
WITH collect(c) as clones
CALL apoc.refactor.rename.label("OldLabel","NewLabel",clones)
YIELD batches as b
RETURN clones
Running them separately works.
The end goal is to remove the filter on id(n) <> 123 and have both calls execute against all nodes with OldLabel. i.e. create a set of OldLabel, clone and rename them.
MATCH (n:OldLabel {id:'abc'})
WITH collect(n) as nodes
CALL apoc.refactor.cloneNodes(nodes,True) YIELD output as c
return c
if 123 is the original node, rename all others.
MATCH (n:OldLabel {id:'abc'})
WHERE id(n) <> 123
WITH collect(n) as clones
CALL apoc.refactor.rename.label("OldLabel","NewLabel",clones)
YIELD batches as b
RETURN clones
11-18-2021 09:12 AM
@czepeda
This is very weird. I think it's a apoc.refactor.rename.label
bug.
Anyway, you could execute this statement as a workaround (using the apoc.periodic.iterate
to batch results):
MATCH (n:OldLabel {id:'abc'})
WITH collect(n) as nodes
CALL apoc.refactor.cloneNodes(nodes,true) YIELD output as c WITH c
CALL apoc.periodic.iterate(
"match (c) return id(c) as id",
"MATCH (n) WHERE id(n) = id set n:NewLabel remove n:OldLabel", // change labels
{params: {c: c}}
)
YIELD batches
RETURN batches
If you don't want to use batches, you can just execute:
MATCH (n:OldLabel {id:'aaa'})
WITH collect(n) as nodes
CALL apoc.refactor.cloneNodes(nodes,true) YIELD output as c
WITH c
SET c:NewLabel remove c:OldLabel // change labels
RETURN c
11-18-2021 03:06 PM
Thank you, Guiseppe!
All the sessions of the conference are now available online