Clone nodes
The APOC library contains a procedure that can be used to clone nodes.
Procedure for cloning nodes and relationships
Qualified Name | Type |
---|---|
|
Procedure |
Examples
The examples below will explain this procedure in more detail.
Clone nodes only
CREATE (f:Foo{name:'Foo'}),(b:Bar{name:'Bar'})
MATCH (f:Foo{name:'Foo'}),(b:Bar{name:'Bar'})
CALL apoc.refactor.cloneNodes([f,b])
YIELD input, output, error
RETURN *
In the above query, input
is the source node id, output
is the node cloned, and error
is the possible error message.
This query will result in the following graph:
Clone nodes and constraints
The below example will demonstrate what happens when apoc.refactor.cloneNodes
is called using nodes with constraints on them.
CREATE CONSTRAINT ON (n:UniqueLabel) ASSERT n.key IS UNIQUE
CREATE (:UniqueLabel {key: 1})
MATCH (n:UniqueLabel)
CALL apoc.refactor.cloneNodes([n])
YIELD error, output
RETURN error, output
The result of the above query will result in an error because of the uniqueness constraint placed on the original node.
error |
---|
output |
"Node(<NNN>) already exists with label `UniqueLabel` and property `key` = 1" |
null |
Skipping properties on cloned nodes
It is possible to exclude propertes when cloning nodes.
This is done by specifying the skipProperties
list in the parameter.
In the below example, the age property of the original node is skipped in the cloned node.
CREATE (f:Foo{name:'Bar', surname: 'Baz', age: 66})
MATCH (n:Foo)
CALL apoc.refactor.cloneNodes([n], false, ["age"])
YIELD output
RETURN properties(output) AS props
props |
---|
{ "surname": "Baz", "name": "Bar" } |