Kelvin U
Kelvin U

Reputation: 353

How to merge two nodes together and retain all links to the nodes in NEO4j / Cypher?

I am trying to merge 2 nodes together in NEO4j.

Each node has it's own independent set of links to nodeA and nodeB.

I want to merge all the attributes between nodeA and nodeB together as they have some different properties. Whilst also retaining all the links to both nodes in the newly merged node.

How would I go about even doing this?

Upvotes: 1

Views: 456

Answers (1)

Brakebein
Brakebein

Reputation: 2237

You should have a look at APOC Procedures. The procedure apoc.refactor.mergeNodes does exactly what you are looking for.

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

Docs and examples: https://neo4j.com/docs/labs/apoc/current/graph-updates/graph-refactoring/merge-nodes/

Upvotes: 2

Related Questions