Pablo Ruiz Ruiz
Pablo Ruiz Ruiz

Reputation: 636

Merge duplicated relationship between nodes

I am using Neo4j Version 3.5.4 and doing the Introduction Course Exercises on the movies dataset. I noticed that I accidentally introduced duplicates in some relationships as shown:

MATCH (p:Person)-[r:ACTED_IN]->(m:Movie)
 WHERE m.title = 'Forrest Gump'
 RETURN p,m,r

enter image description here enter image description here

How can I combine them so I just keep one of the duplicates?

Thanks!

Upvotes: 0

Views: 42

Answers (1)

jose_bacoy
jose_bacoy

Reputation: 12704

You can use tail and collect functions to remove the duplicates:

  • Collect will get all relationships
  • Tail will get all items in the collection EXCEPT the first one
  • Foreach will do a loop in all items in collection | then delete it

Here it is:

MATCH (p:Person)-[r:ACTED_IN]->(m:Movie)
 WHERE m.title = 'Forrest Gump'
 WITH tail(collect(r)) as coll , p
 FOREACH (x in coll| DELETE x)

Upvotes: 1

Related Questions