Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Duplicate Node from a Node in Neo4j?

Tags:

neo4j

I am copying part of my Main Neo4j Graph(mainDB) into another Graph (dupDB), while doing so how can I create a Node in dupDB that has similar properties as one in mainDB.

I would do

Node main = mainDB.getNodeByID(477);
Node dup = dupDB.createNode();

Now I have to copy each property in main to dup manually, is there any one-line method to do this?

like image 585
Sravan Avatar asked Jun 27 '12 08:06

Sravan


1 Answers

You can create a duplicate with a map in Neo4j 2.1 (not sure of earlier)

MATCH (n:Node {name: 'abc'}) 
WITH n AS map 
CREATE (copy:Node) 
SET copy=map
RETURN copy

If you have a uniqueness constraint on any of the properties it will fail though with the message...

Node already exists with label XX and property "property"=[value]

You can avoid that by supplying a new value for the property with the uniqueness constraint, creating the new node and copying the other non-unique property values from the original node.

MATCH (n:Node {name: 'abc'}) 
WITH n as map 
CREATE (copy:Node {name: 'def'}) 
SET copy.property1 = map.property1
, copy.property2 = map.property2
RETURN copy
like image 189
Dave Bennett Avatar answered Oct 27 '22 17:10

Dave Bennett