# Can we merge multiple Nodes based on Single common property

**URL:** <https://community.neo4j.com/t/can-we-merge-multiple-nodes-based-on-single-common-property/3181>\
**Category:** Browser\
**Tags:** cypher\
**Created:** [November 22, 2018, 5:08am UTC](https://community.neo4j.com/t/can-we-merge-multiple-nodes-based-on-single-common-property/3181 "2018-11-22T05:08:59Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![groverjatin17](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/groverjatin17/32/2174_2.png) [@groverjatin17](https://community.neo4j.com/u/groverjatin17)\
**Post date:** [November 22, 2018, 5:08am UTC](https://community.neo4j.com/t/can-we-merge-multiple-nodes-based-on-single-common-property/3181/1 "2018-11-22T05:08:59Z")

</div>

Hi All,

I am relatively new to neo4j and I am working on 1 Use case where we are trying to merge all nodes(with 1 common property, such as all nodes with year= "1995") into 1 node where all the relationships are heading towards it rather than 3 different nodes.

For example:-  
I have 3 csv files with below type of data.

Actor.csv:-

| Actor Id | Name |
| --- | --- |
| 1 | Sam |

Movie.csv:-

| Movie Id | Movie Name | Year | Actor Id | Director Id |
| --- | --- | --- | --- | --- |
| 45 | Avengers | 2016 | 1 | 10 |
| 23 | Movie 2 | 2016 | 1 | 10 |
| 12 | Movie 3 | 2016 | 1 | 10 |

Director.csv:-

| Director ID | Director Name |
| --- | --- |
| 10 | Danny Morgan |

Now if I use the following Cypher Query:-

> Match (actor:Actor)-[:ACTED\_IN]-\>(movie:Movie)\<-[:DIRECTED\_BY]-(director:Director)  
> return actor, movie, director

Now the graph that appears have 1 node of actor and 3 nodes of movie then 1 node of director.  
all connected via relationships.

I want there to be only 1 node of movie displaying **"2016"** as year and 3 edges/relationships going out of actor to the movie node and 3 edges/relationships merging as 1 into Director node  
or  
1 single relationship of **"ACTED\_IN"** going towards movie but a number **"3"** (suggesting there are 3 relationships for that year) written over it

The main goal is to show that there are 3 realtionships related to the year 2016.  
I know we can perform aggregations to return tables with count or arrays of data.  
But can we do it in graph nodes as well.

NOTE:-I am using Neo4j Browser version 3.1.4

Thank you for reading I hope it's not a silly question.

---

<div class="post-metadata">

**Author:** ![mike\_r\_black](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/mike_r_black/32/30089_2.png) [@mike\_r\_black](https://community.neo4j.com/u/mike_r_black)\
**Post date:** [November 22, 2018, 5:25am UTC](https://community.neo4j.com/t/can-we-merge-multiple-nodes-based-on-single-common-property/3181/2 "2018-11-22T05:25:21Z")

</div>

Check out the [APOC Merge Nodes](https://neo4j-contrib.github.io/neo4j-apoc-procedures/#_merge_nodes)

---

<div class="post-metadata">

**Author:** ![groverjatin17](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/groverjatin17/32/2174_2.png) [@groverjatin17](https://community.neo4j.com/u/groverjatin17)\
**Post date:** [November 22, 2018, 10:54am UTC](https://community.neo4j.com/t/can-we-merge-multiple-nodes-based-on-single-common-property/3181/3 "2018-11-22T10:54:38Z")

</div>

HI Mike.  
Thanks for the reply.  
I followed exact same steps mentioned.

I created 2 nodes:  
Here "Foo" have 202 as id and "Bar" have 203 as id.

`CREATE (f:Sample {name:'Foo'}), (b:Sample {surname:'Bar'}) RETURN f,b`

Then i run the following CYPHER command:-

```auto
MATCH (f:Sample {name:'Foo'}), (b:Sample {surname:'Bar'})
CALL apoc.refactor.mergeNodes([f,b])
YIELD node RETURN node

```

I get the following error but i do not understand why am i getting it and what to make of it.

> Neo.ClientError.Statement.EntityNotFound: Node with id 203 has been deleted in this transaction

---

<div class="post-metadata">

**Author:** ![12kunal34](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/12kunal34/32/2617_2.png) [@12kunal34](https://community.neo4j.com/u/12kunal34)\
**Post date:** [November 22, 2018, 11:19am UTC](https://community.neo4j.com/t/can-we-merge-multiple-nodes-based-on-single-common-property/3181/4 "2018-11-22T11:19:04Z")

</div>

Hey,  
please try once below query

```auto
MATCH (f:Sample {name:'Foo'}),(b:Sample {surname:'Bar'})
WITH * LIMIT 1
call apoc.refactor.mergeNodes([f,b]) yield node
return "none"

```

---

<div class="post-metadata">

**Author:** ![groverjatin17](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/groverjatin17/32/2174_2.png) [@groverjatin17](https://community.neo4j.com/u/groverjatin17)\
**Post date:** [November 22, 2018, 1:06pm UTC](https://community.neo4j.com/t/can-we-merge-multiple-nodes-based-on-single-common-property/3181/5 "2018-11-22T13:06:51Z")

</div>

It did nothing. Just showed this:

| none |
| --- |
| "none" |

Nothing else.

---

<div class="post-metadata">

**Author:** ![12kunal34](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/12kunal34/32/2617_2.png) [@12kunal34](https://community.neo4j.com/u/12kunal34)\
**Post date:** [November 22, 2018, 2:23pm UTC](https://community.neo4j.com/t/can-we-merge-multiple-nodes-based-on-single-common-property/3181/6 "2018-11-22T14:23:23Z")

</div>

Now search for the node .  
It must present in your database.  
If it executed well that means merging is complete

---

<div class="post-metadata">

**Author:** ![groverjatin17](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/groverjatin17/32/2174_2.png) [@groverjatin17](https://community.neo4j.com/u/groverjatin17)\
**Post date:** [November 23, 2018, 4:59am UTC](https://community.neo4j.com/t/can-we-merge-multiple-nodes-based-on-single-common-property/3181/7 "2018-11-23T04:59:55Z")

</div>

wow. It worked.  
Thanks.

Can you plz tell why it did not work in previous case. And what did we do differently or the analogy of your solution.

---

<div class="post-metadata">

**Author:** ![groverjatin17](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/groverjatin17/32/2174_2.png) [@groverjatin17](https://community.neo4j.com/u/groverjatin17)\
**Post date:** [November 23, 2018, 5:32am UTC](https://community.neo4j.com/t/can-we-merge-multiple-nodes-based-on-single-common-property/3181/8 "2018-11-23T05:32:05Z")

</div>

Hi Again,  
It worked fine when we have to merge 2 nodes of same `Label` but with different `Properties`.  
Like properties `Name` and `Surname` under common label ` **Sample** ` in above comments.

But in my case where properties are common.

For Example:-  
Label ABC have 40 nodes with following properties.

```auto
{
sid:1 to 40 //Unique for all 40 nodes
counterparty: "Harley Davidson" //same for all 40 nodes.
clientName:"Indian Motors" //Also same for all 40 nodes
}

```

I used this:-

```auto
MATCH (f:ABC{counterparty:'Harley Davidson'}),(b:ABC{counterparty:'Harley Davidson'})
WITH * LIMIT 1
call apoc.refactor.mergeNodes([f,b]) yield node
return "none"

```

I tried to merge 40 nodes with 1 property as common among all. It executed fine displaying none as output just like previously. But still I see there are 40 nodes under ABC instead of 1.

---

<div class="post-metadata">

**Author:** ![michael.hunger](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/michael.hunger/32/27377_2.png) [@michael.hunger](https://community.neo4j.com/u/michael.hunger)\
**Post date:** [November 23, 2018, 6:28am UTC](https://community.neo4j.com/t/can-we-merge-multiple-nodes-based-on-single-common-property/3181/10 "2018-11-23T06:28:19Z")

</div>

Which version do you use?

---

<div class="post-metadata">

**Author:** ![groverjatin17](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/groverjatin17/32/2174_2.png) [@groverjatin17](https://community.neo4j.com/u/groverjatin17)\
**Post date:** [November 23, 2018, 6:38am UTC](https://community.neo4j.com/t/can-we-merge-multiple-nodes-based-on-single-common-property/3181/11 "2018-11-23T06:38:38Z")

</div>

I am using  
Neo4j Browser version 3.2.10  
Neo4j Server Version 3.4.9(community)  
APOC :- apoc-3.4.0.3-all.jar

---

<div class="post-metadata">

**Author:** ![michael.hunger](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/michael.hunger/32/27377_2.png) [@michael.hunger](https://community.neo4j.com/u/michael.hunger)\
**Post date:** [November 23, 2018, 6:52am UTC](https://community.neo4j.com/t/can-we-merge-multiple-nodes-based-on-single-common-property/3181/12 "2018-11-23T06:52:41Z")

</div>

It doesn't work like that (pairwise merges) as it would do random pairs and then randomly try to merge them where half of them are already gone.

```auto
MATCH (f:ABC)
WITH f.counterparty as counterparty, f.clientName as clientName, collect(f) as nodes
WHERE size(nodes) > 1
call apoc.refactor.mergeNodes(nodes) yield node 
return count(*)

```

---

<div class="post-metadata">

**Author:** ![groverjatin17](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/groverjatin17/32/2174_2.png) [@groverjatin17](https://community.neo4j.com/u/groverjatin17)\
**Post date:** [November 23, 2018, 7:06am UTC](https://community.neo4j.com/t/can-we-merge-multiple-nodes-based-on-single-common-property/3181/13 "2018-11-23T07:06:25Z")

</div>

Great Sir. It worked.  
Tons of thanks.  
I need to deep dive rather than just learning match, create and where clauses of Neo4j.  
Only if my manager give me time to learn..lol

Again,  
Thanks everybody for their input 🙂

---

<div class="post-metadata">

**Author:** ![elena](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/elena/32/13074_2.png) [@elena](https://community.neo4j.com/u/elena)\
**Post date:** [March 4, 2020, 6:58pm UTC](https://community.neo4j.com/t/can-we-merge-multiple-nodes-based-on-single-common-property/3181/14 "2020-03-04T18:58:58Z")

</div>

Updated docs links:

[http://neo4j-contrib.github.io/neo4j-apoc-procedures/3.5/graph-refactoring/merge-nodes/](http://neo4j-contrib.github.io/neo4j-apoc-procedures/3.5/graph-refactoring/merge-nodes/)  
[https://neo4j.com/docs/labs/apoc/current/graph-updates/graph-refactoring/merge-nodes/](https://neo4j.com/docs/labs/apoc/current/graph-updates/graph-refactoring/merge-nodes/)
