查询在没有克隆过程的情况下复制具有 Neo4j 关系的节点

query to copy a node with relations in neo4j without the clone procedure

我想复制一个节点及其所有属性以及与其他节点的关系。我使用的是 Neo4j 版本 3.4.7,所以我不能使用克隆过程。我的节点可能有不同类型的关系,我想复制它们。我想使用这个查询

match (map:student {name:'test'})  create (copy:student) set copy=map with copy,map match (map)-[r1]->(n) with collect(r1) as rels,map,copy,n foreach( rel in rels | create (copy)-[r2:type(rel)]->(n) set r2+=rel) return copy,n

但我在 foreach 中使用 type(rel) 时遇到无效语法错误。有没有办法在不知道关系类型的情况下做到这一点?

最简单的方法是install the APOC library, and call the procedure apoc.refactor.cloneNodesWithRelationships, as documented here

在您的情况下,您的查询将变为:

MATCH (map:student {name:'test'})
CALL apoc.refactor.cloneNodesWithRelationships([map])

注意:节点标签通常以大写字母开头(Student 在 Neo4j 数据集中比 student 更常见)。