使用变量在密码中创建关系? [英] Using variable to create relationship in cypher?
问题描述
我正在尝试动态创建节点之间的关系.我遇到的问题是我无法使用变量指定关系类型.
I'm trying to create a relationship between nodes dynamically. The problem I am having is that I am unable to use a variable to specify the relationship type.
例如,我有数据:
{
nodes: [
{
"name":"Node1"
},
...
],
relationships: [
{
"sourceNode": "Node1",
"destinationNode": "Node2",
"relationshipType": "FRIEND"
},
...
]
}
假设所有节点都已创建.
Assume all nodes have been created.
我现在想在类型为relationshipType
的节点之间创建关系.
I now want to create relationships between nodes of type relationshipType
.
我正在尝试这样做:
WITH {json} AS document
UNWIND document.relationships AS relationship
MATCH (pdt:Node {name: relationship.sourceNode})
MATCH (cdt:Node {name: relationship.destinationNode})
CREATE (pdt)-[r:relationship.relationshipType]->(cdt)
RETURN pdt.name,type(r),cdt.name
但是它会出现在[r:relationship.relationshipType]
上,因为它期望像[r:CHILD]
这样的显式类型.
However it craps out at [r:relationship.relationshipType]
because it is expecting an explicit type like [r:CHILD]
.
是否可以使用变量设置关系类型?
Is it possible to use a variable to set a relationship type?
推荐答案
安装 APOC插件,您可以使用apoc.create.relationship
过程创建具有动态类型的关系.
After installing the APOC plugin, you can use the apoc.create.relationship
procedure to create relationships with dynamic types.
例如:
WITH {json} AS document
UNWIND document.relationships AS relationship
MATCH (pdt:Node {name: relationship.sourceNode})
MATCH (cdt:Node {name: relationship.destinationNode})
CALL apoc.create.relationship(pdt, relationship.relationshipType, NULL, cdt) YIELD rel
RETURN pdt.name, type(rel), cdt.name
这篇关于使用变量在密码中创建关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!