Neo4j Cypher: copy relationships and delete node

后端 未结 2 1389
有刺的猬
有刺的猬 2021-01-23 23:45

I\'m trying to copy all inward relationships of a node (n) to another node (m) (both of witch I know the ids) before deleting (n), but I c

相关标签:
2条回答
  • 2021-01-23 23:56

    assuming all incoming relationships are of same type say 'foo'. Then you could do the shorter following query:

    START n=node(id1),m=node(id2) 
    MATCH n<-[r:foo]-(p) 
    WITH collect(p) as endNodes,m
    FOREACH(mm in endNodes | CREATE m-[:foo]->mm)
    

    Which avoids the double foreach

    0 讨论(0)
  • 2021-01-24 00:17

    You wont be able to create a relationshiptype dynamically from inside a collection of relationships.

    Suppose even if we collect all the incoming relationships as below

    START n=node(id1) MATCH n<-[r]-() WITH collect(r) as rels ...
    

    You would be able to iterate over the collection rels but WOULD NOT be able to do below

    CREATE (n)-[rels[i]]->(m)
    

    So assuming if all incoming relationships are of same type say 'foo'. Then you could do the following.

    START n=node(id1),m=node(id2) 
    MATCH n<-[r:foo]-(p) 
    WITH collect(p) as endNodes,m
    FOREACH(i in range(0,length(endNodes)-1) | foreach(a in [endNodes[i]] | 
     create m<-[:foo]-a 
    ))
    

    In case if your relationshipTypes are different, then you can refer this workaround technique :here. You can query from the console, download all startnode , endnode, relationshiptype info as csv into excel sheet. And then run cypher script to run from it.

    Other way is you can query using java api for neo4j and then store all the relationships and nodes , frame your queries accordingly and fire back again.

    0 讨论(0)
提交回复
热议问题