Neo4j Cypher:复制关系并删除节点 [英] Neo4j Cypher: copy relationships and delete node

查看:163
本文介绍了Neo4j Cypher:复制关系并删除节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在删除(n)之前将节点(n)的所有向内关系复制到另一个节点(m)(我都知道ID),但是我无法提供代码. 这些关系可能存在或可能不存在.

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 couldn't come up with the code. The relationships may or may not exist.

有人片段吗?

推荐答案

您将无法从一组关系中动态创建一个关系类型.

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)

因此,假设所有传入关系都是相同类型,请说"foo".然后,您可以执行以下操作.

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 
))

如果您的RelationshipTypes不同,则可以参考这种解决方法:此处.您可以从控制台查询,将所有startnode,endnode,RelationshipType信息以csv格式下载到excel工作表中.然后运行cypher脚本以从中运行.

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.

其他方法是,您可以使用Java api查询neo4j,然后存储所有关系和节点,相应地构建查询框架,然后再次触发.

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.

这篇关于Neo4j Cypher:复制关系并删除节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆