Neo4j Cypher : transfer all relationships before replacing a node by another

大憨熊 提交于 2020-01-04 03:25:22

问题


I'm trying to transfert all ingoing and outgoing relationships from a node to another, before deleting the first one. they both have the same label. I saw this Neo4j Cypher: copy relationships and delete node but in my case i don't know the type of the relations and i want to transfer both ingoing and outgoing ones.

i'm looking for either a cypher query or a query based on neo4j.rb


回答1:


I don't think that this is possible with pure cypher. Here's a solution using neo4j.rb that I think will work:

# Assuming node1 already loaded
node_query = Neo4j::Session.query.match(node: {neo_id: node1.neo_id})

types = node_query.match('node-[rel]-()').pluck('DISTINCT type(rel)')

types.each do |type|
  node_query.match('node-[rel]->(other)').with(:node, :rel, :other).create("node-[new_rel]->other").set('new_rel = rel').exec
  node_query.match('node<-[rel]-(other)').with(:node, :rel, :other).create("node<-[new_rel]-other").set('new_rel = rel').exec
end


来源:https://stackoverflow.com/questions/28254800/neo4j-cypher-transfer-all-relationships-before-replacing-a-node-by-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!