# Duplicate a subgraph

**URL:** <https://community.neo4j.com/t/duplicate-a-subgraph/55204>\
**Category:** Cypher\
**Tags:** apoc, performance, cypher\
**Created:** [April 23, 2022, 7:18am UTC](https://community.neo4j.com/t/duplicate-a-subgraph/55204 "2022-04-23T07:18:37Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![verachkaverachk](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/verachkaverachk/32/23510_2.png) [@verachkaverachk](https://community.neo4j.com/u/verachkaverachk)\
**Post date:** [April 23, 2022, 7:18am UTC](https://community.neo4j.com/t/duplicate-a-subgraph/55204/1 "2022-04-23T07:18:38Z")

</div>

Hello,  
can I duplicate the following graph in neo4j? and if it is possible, then what cyphers can can me?

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/4/e/4e1fdea0a940f4e4e3e486db7d417005d921c649.png)  
Have a lovely day :)

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [April 23, 2022, 7:26am UTC](https://community.neo4j.com/t/duplicate-a-subgraph/55204/2 "2022-04-23T07:26:17Z")

</div>

Hello @verachkaverachk 🙂

If you want to duplicate this graph in another database?

You can use the [Graph Data Science](https://neo4j.com/docs/graph-data-science/current/) plugin.

First, you create the in-memory graph:

```auto
CALL gds.graph.project('graph', '*', '*')
YIELD graphName AS graph, nodeProjection, nodeCount AS nodes, relationshipProjection, relationshipCount AS rels

```

Then, you export the graph to a new database:

```auto
CALL gds.graph.export('graph', { dbName: 'mydatabase' })

```

Finally, you can create the new database and use it:

```auto
:use system
CREATE DATABASE mydatabase;
:use mydatabase

```

Regards,  
Cobra

---

<div class="post-metadata">

**Author:** ![verachkaverachk](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/verachkaverachk/32/23510_2.png) [@verachkaverachk](https://community.neo4j.com/u/verachkaverachk)\
**Post date:** [April 23, 2022, 9:43am UTC](https://community.neo4j.com/t/duplicate-a-subgraph/55204/3 "2022-04-23T09:43:33Z")

</div>

thank you so much for your answers 🙂  
and if I want to duplicate this graph in the same databse? is it possible?  
and if it is, then which query can help me?

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [April 23, 2022, 11:41am UTC](https://community.neo4j.com/t/duplicate-a-subgraph/55204/4 "2022-04-23T11:41:19Z")

</div>

You can have a look at APOC plugin with [clone nodes functions](https://neo4j.com/labs/apoc/4.3/graph-updates/graph-refactoring/clone-nodes/) or [clone subgraph functions](https://neo4j.com/labs/apoc/4.3/graph-updates/graph-refactoring/clone-subgraph/). I think the `apoc.refactor.cloneNodesWithRelationships()` function is what you need.

Maybe this query will help you:

```auto
MATCH (n)
CALL apoc.refactor.cloneNodesWithRelationships([n])
YIELD input, output
RETURN *

```

---

<div class="post-metadata">

**Author:** ![verachkaverachk](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/verachkaverachk/32/23510_2.png) [@verachkaverachk](https://community.neo4j.com/u/verachkaverachk)\
**Post date:** [May 10, 2022, 2:09pm UTC](https://community.neo4j.com/t/duplicate-a-subgraph/55204/5 "2022-05-10T14:09:16Z")

</div>

Hello ,I've installed the APOC library, and tried the suggested query.  
this one:  
MATCH (n)  
CALL apoc.refactor.cloneNodesWithRelationships([n])  
YIELD input, output  
RETURN \*what i've tried to get was this whole graph twice , like to saparated similar graphs:

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/9/9/995e53109f7c2cdff33bfd1f7565158059dfe716.png)  
what i've actually got is something weird that looks like this:  
 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/2/a/2a80c00606d170a0e741a16d0d1b3d3b8627651e.jpeg)

What I try to do is find a query that will duplicate my original graph and i'll gey two of these.  
What query can I do to solve it?  
have a great day.  
Vera

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [May 10, 2022, 2:14pm UTC](https://community.neo4j.com/t/duplicate-a-subgraph/55204/6 "2022-05-10T14:14:44Z")

</div>

Can you share your dataset please?

---

<div class="post-metadata">

**Author:** ![verachkaverachk](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/verachkaverachk/32/23510_2.png) [@verachkaverachk](https://community.neo4j.com/u/verachkaverachk)\
**Post date:** [May 10, 2022, 5:00pm UTC](https://community.neo4j.com/t/duplicate-a-subgraph/55204/7 "2022-05-10T17:00:57Z")

</div>

Where get I get it from?(the dataSet) so I can send it here?

---

<div class="post-metadata">

**Author:** ![verachkaverachk](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/verachkaverachk/32/23510_2.png) [@verachkaverachk](https://community.neo4j.com/u/verachkaverachk)\
**Post date:** [May 11, 2022, 7:04am UTC](https://community.neo4j.com/t/duplicate-a-subgraph/55204/8 "2022-05-11T07:04:53Z")

</div>

I can try to export the data I have to one of these:

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/3/c/3c3e520ca5b14f8bfdfacc0e1fa2276ff62e73d2.png)

or dataSet means something else?

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [May 11, 2022, 7:18am UTC](https://community.neo4j.com/t/duplicate-a-subgraph/55204/9 "2022-05-11T07:18:16Z")

</div>

The dataset is the data, can you share queries to recreate the graph?

---

<div class="post-metadata">

**Author:** ![verachkaverachk](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/verachkaverachk/32/23510_2.png) [@verachkaverachk](https://community.neo4j.com/u/verachkaverachk)\
**Post date:** [May 11, 2022, 8:26am UTC](https://community.neo4j.com/t/duplicate-a-subgraph/55204/10 "2022-05-11T08:26:15Z")

</div>

I create the graph with the help of java (Springboot Dao) it starts with an entity of CellType that represents the type of a node I'm going to use(operation or input ) and with the first celltype(that must be a type of input) I connect it to a "real cell" and I call it "cellInstaces".(they also will appear in my frontend later without all the attributes). This cellInstance is going to be connected to another cellInstance from Celltype(again cellInstance must have a type-cellType) of function because in the end my purpose is to run a simulator on this graph and calculate stuff from function to function. Then, I connect an entity named a subgraphInstance to cellInstance(that represents the input)to my cellInstance. and all the nodes that are connected to this first cellInstance we will see them.  
the query I use to create cell instance is this one

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/b/1/b175822c87495b49b365e6983a2b6334e9b40499.png)  
The query to connect between cellInstances is  
 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/8/0/8038f25fc359a14c54e62474c15f23d5e8ccb435.png)  
I did use a query when I wanted to get my graph that looks like this:  
 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/7/c/7c9f702698a1e3df4f511309f9a340a8fcf4b774.png)

---

<div class="post-metadata">

**Author:** ![verachkaverachk](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/verachkaverachk/32/23510_2.png) [@verachkaverachk](https://community.neo4j.com/u/verachkaverachk)\
**Post date:** [May 11, 2022, 9:27am UTC](https://community.neo4j.com/t/duplicate-a-subgraph/55204/11 "2022-05-11T09:27:03Z")

</div>

and if you meant they tables that represents the data of this graph for example:

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/c/a/ca12b5612b9adea528dc84c17c2ddcb8afc83324.png)  
it look like this:  
{  
"identity": 0,  
"labels": [  
"CellInstance"  
],  
"properties": {  
"yCoordinate": 5.6,  
"xCoordinate": 3.7  
}  
}  
{  
"identity": 20,  
"labels": [  
"SubGraphInstance"  
],  
"properties": {  
"name": "Graph1"  
}  
}  
{  
"identity": 77,  
"labels": [  
"Variable"  
],  
"properties": {  
"variableName": "x"  
}  
}  
{  
"identity": 78,  
"labels": [  
"Function"  
],  
"properties": {  
"expression": "x"  
}  
}  
{  
"identity": 79,  
"labels": [  
"CellType"  
],  
"properties": {  
"name": "Input Cell",  
"transformType": "INPUT\_TO\_ANALOG"  
}  
}  
{  
"identity": 84,  
"labels": [  
"CellInstance"  
],  
"properties": {  
"yCoordinate": 6.6,  
"xCoordinate": 4.7  
}  
}  
{  
"identity": 85,  
"labels": [  
"Variable"  
],  
"properties": {  
"variableName": "x"  
}  
}  
{  
"identity": 86,  
"labels": [  
"Variable"  
],  
"properties": {  
"variableName": "y"  
}  
}  
{  
"identity": 87,  
"labels": [  
"Function"  
],  
"properties": {  
"expression": "x+y"  
}  
}  
{  
"identity": 88,  
"labels": [  
"CellType"  
],  
"properties": {  
"name": "Addition ",  
"transformType": "ANALOG\_TO\_ANALOG"  
}  
}  
{  
"identity": 89,  
"labels": [  
"CellInstance"  
],  
"properties": {  
"yCoordinate": 6.6,  
"xCoordinate": 4.7  
}  
}  
{  
"identity": 90,  
"labels": [  
"Connection"  
],  
"properties": {  
"delay": 1  
}  
}  
{  
"identity": 91,  
"labels": [  
"Connection"  
],  
"properties": {  
"delay": 1  
}  
}

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [May 11, 2022, 10:47am UTC](https://community.neo4j.com/t/duplicate-a-subgraph/55204/12 "2022-05-11T10:47:48Z")

</div>

You can try this query to duplicate all nodes and their relationships:

```auto
MATCH (n)
WITH collect(n) AS nodes
CALL apoc.refactor.cloneSubgraph(nodes)
YIELD input, output, error
RETURN input, output, error;

```

You can also add a `WHERE` clause to select nodes and their relationships from of a subgraph:

```auto
MATCH (n)
WHERE n.name = "Graph1"
WITH collect(n) AS nodes
CALL apoc.refactor.cloneSubgraph(nodes)
YIELD input, output, error
RETURN input, output, error;

```

There is more documentation [here](https://neo4j.com/labs/apoc/4.4/overview/apoc.refactor/apoc.refactor.cloneSubgraph/).

---

<div class="post-metadata">

**Author:** ![verachkaverachk](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/verachkaverachk/32/23510_2.png) [@verachkaverachk](https://community.neo4j.com/u/verachkaverachk)\
**Post date:** [May 11, 2022, 11:17am UTC](https://community.neo4j.com/t/duplicate-a-subgraph/55204/13 "2022-05-11T11:17:15Z")

</div>

I will try it and let you know today! ,thank you so so much for trying to help me understand !! 🙂

---

<div class="post-metadata">

**Author:** ![verachkaverachk](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/verachkaverachk/32/23510_2.png) [@verachkaverachk](https://community.neo4j.com/u/verachkaverachk)\
**Post date:** [May 11, 2022, 1:38pm UTC](https://community.neo4j.com/t/duplicate-a-subgraph/55204/14 "2022-05-11T13:38:02Z")

</div>

yeahi! it works!  
thank you so much for helping me ❤

---

<div class="post-metadata">

**Author:** ![verachkaverachk](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/verachkaverachk/32/23510_2.png) [@verachkaverachk](https://community.neo4j.com/u/verachkaverachk)\
**Post date:** [May 18, 2022, 9:18am UTC](https://community.neo4j.com/t/duplicate-a-subgraph/55204/15 "2022-05-18T09:18:08Z")

</div>

Hello!  
Can I duplicate my graph by his name or id with a query?for example if I have a few different subgraphs(When I say subgraphisntance i mean the node and all his connected elements) and I want to duplicate all the elements of a specific subgraph in my DB and not duplicate them all, is there a way to do it with apoc? Like you showed me with the first query but this time finding the whole subgraph by his id or name?  
Vera

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [May 18, 2022, 10:19am UTC](https://community.neo4j.com/t/duplicate-a-subgraph/55204/16 "2022-05-18T10:19:21Z")

</div>

Hello @verachkaverachk 🙂

It's in the answer I gave you, the second query with the `WHERE` clause should do what you want.

Regards,  
Cobra

---

<div class="post-metadata">

**Author:** ![verachkaverachk](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/verachkaverachk/32/23510_2.png) [@verachkaverachk](https://community.neo4j.com/u/verachkaverachk)\
**Post date:** [May 18, 2022, 10:24am UTC](https://community.neo4j.com/t/duplicate-a-subgraph/55204/17 "2022-05-18T10:24:23Z")

</div>

I tried the second one, and got only 1 duplicated node which was Graph1 that represents subGraphInstance, my problem is that i got it without all the connected elements of this node and I need them too

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [May 18, 2022, 11:22am UTC](https://community.neo4j.com/t/duplicate-a-subgraph/55204/18 "2022-05-18T11:22:47Z")

</div>

All the nodes of the subgraph must be tagged with the property, not only one node.

---

<div class="post-metadata">

**Author:** ![verachkaverachk](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/verachkaverachk/32/23510_2.png) [@verachkaverachk](https://community.neo4j.com/u/verachkaverachk)\
**Post date:** [May 18, 2022, 5:26pm UTC](https://community.neo4j.com/t/duplicate-a-subgraph/55204/19 "2022-05-18T17:26:09Z")

</div>

Can you please elaborate how will the second query look like if will tag the other nodes there??

---

<div class="post-metadata">

**Author:** ![verachkaverachk](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/verachkaverachk/32/23510_2.png) [@verachkaverachk](https://community.neo4j.com/u/verachkaverachk)\
**Post date:** [May 18, 2022, 5:41pm UTC](https://community.neo4j.com/t/duplicate-a-subgraph/55204/20 "2022-05-18T17:41:45Z")

</div>

Also is there a way to create a query where I can choose specific nodes from my graph and duplicate them and get their connections ?

[Next page](https://community.neo4j.com/t/duplicate-a-subgraph/55204.md?page=2)
