sanjayr
sanjayr

Reputation: 1949

Merge nodes with different labels into new node with both labels

I have a Neo4J database with a bunch of employee and consultant nodes, with a relationship consults pointing from a consultant to an employee node. A consultant can consult many employees and an employee can have multiple consultants.

My issue is that some (not all!) of the consultants are employees as well. How do I go about merging nodes to have two labels to specify those consultants that are employees?

I exported my data from Postgres and imported it to Neo so I have a bunch of nodes like the examples below:

The name field on all the nodes is unique. Is there a way to match nodes with the same name, create a new node with the new title, and delete the old nodes?

(c:Consultant {name:“Consultant1”}) (e:Employee {name:“Consultant1"})

Desired fix: (p:Consultant:Employee {name:“Consultant1”)

Upvotes: 1

Views: 508

Answers (1)

cybersam
cybersam

Reputation: 67019

The APOC procedure apoc.refactor.mergeNodes should work for your use case.

It merges multiple nodes from a list into the first node, and also merges all their relationships as well.

Upvotes: 1

Related Questions