0
votes

i have a node (a) , i want to create new node (c) and relate it to all nodes related to (a) with the same old relationships (a) related to other nodes in the graph??

i tried the following cypher :

MATCH (a)-[r]-(b) where ID(a)=  42
WITH COLLECT(r) AS rels, a, b
create (c) set c.name='علامات'
FOREACH (rel in rels |
       CREATE (b)-[r:LINKED_TO]->(c)
       SET r+=rel
)

but it created multiple (c) nodes not just one.

i tried the cypher below but it created multiple (c) nodes with new relations not only one node.

MATCH (a)-[r]-(b) where ID(a)=  42
WITH COLLECT(r) AS rels, a, b
create (c) set c.name='علامات'
FOREACH (rel in rels |
       CREATE (b)-[r:LINKED_TO]->(c)
       SET r+=rel
)
2

2 Answers

0
votes

You can use the APOC procedure apoc.refactor.cloneNodesWithRelationships.

For example:

MATCH (a)
WHERE ID(a) = 42
CALL apoc.refactor.cloneNodesWithRelationships([a]) YIELD input, output
RETURN input, output
0
votes

You can try to run a following query

CREATE (clone {name: "علامات"})
WITH clone
MATCH (a)-[r]-(b) where ID(a)=  42
WITH COLLECT(r) AS rels, collect(b) as nodes, clone
WITH rels, nodes, clone, range(0, size(rels)-1) AS indexes
UNWIND indexes AS i
WITH clone, rels[i] as rel, nodes[i] as relNode
create (clone)-[nr:LINKED_TO]->(relNode) SET nr += rel

It will create a new node, relations accordingly, but it is not a generic query, and it will not copy labels and direction of relationships. To have more universal query it is better to duplicate everything with APOC and change properties of created node afterwards.