当节点存在时创建关系,否则创建新节点

时间:2017-12-04 12:29:41

标签: neo4j cypher

我有2种类型的节点员工和员工所在的客户 StaffList

{
  "name": "vipul",
  "cprNumber": 121
}
,{
  "name": "amit",
  "cprNumber": 123
}

客户端列表

{
  "property1": "pptVal",
  "cprNumber": 121
}

现在我想创建这两个节点之间的关系,就像在第一种情况下CPR编号匹配所以关系为vipul创建“BELONGS_TO”,但是从不存在,amit的cpr编号存在任何节点,因此新节点需要要创建和创建的关系。

1 个答案:

答案 0 :(得分:1)

我认为您可以使用MERGE来实现目标:

// Match Vipul and Amit nodes
MATCH (a:Client)
// Match :ClientList node when it has cprNumber = a.cprNumber.
// When no node is matched, create it.
MERGE (b:ClientList {cprNumber : a.cprNumber})
// Create :BELONGS_TO relationship
CREATE (a)-[:BELONGS_TO]->(b)
相关问题