My question is.. given two nodes, node A & node B, is there a cypher query that removes all of the relationships from node A and attaches them instead to node B, so that all nodes that were previously attached to node A, are now attached to node B?
I'm sure there are many use cases for such a query, but my use case is to do with merging multiple social network logins:
Given that I have a member account (member1) using google as a sign in provider And I have a separate member account (member2) using facebook as a sign in provider When member1 attempts to connect to the same facebook account that member2 is using as a sign in provider And member1 requests a merge (merge account) And member2 confirms the merge Then the accounts for member1 and member2 will be merged And one member will be remaining using both google and facebook as a signin provider.
What is the cypher query to do this?
EDIT: there is now way, to specify relationship type dynamically. So, workaround below is limited to relationships with same type and with no properties.
Let's create some data:
CREATE (n1:Node1)
CREATE (n2:Node2)
CREATE (target1:Target)
CREATE (target2:Target)
CREATE (target3:Target)
CREATE (n1)-[:REL]->(target1)
CREATE (n1)-[:REL]->(target2)
CREATE (n1)-[:REL]->(target3);
We have such data right now:

Now we want to "move" relationship from Node1 to Node2
Query:
MATCH (n1:Node1)-[r:REL]->(target)
MATCH (n2:Node2)
CREATE (n2)-[:REL]->(target)
DELETE r
And result is:

What we basically are done is:
I know this thread is old but I googled the same question and didn't found a solution on StackOverflow. So here it is.
As Michael told me here You can use APOC functions :
apoc.refactor.to(relations, destination_node)
apoc.refactor.from(relations, destination_node)
And have something like :
MATCH(n2:Resource {name: 'destionation-resource'})
MATCH(n:Resource {name:'source-resource'})<-[r]-()
OPTIONAL MATCH(n)-[r2]->()
CALL apoc.refactor.to(r, n2) YIELD input, output
CALL apoc.refactor.from(r2, n2) YIELD input AS i, output AS o
RETURN *
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With