【问题标题】:create relation when node exist else create a new node当节点存在时创建关系否则创建一个新节点
【发布时间】:2017-12-04 12:29:40
【问题描述】:

我有 2 种类型的节点人员和客户端,其中人员有 员工名单

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

客户列表

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

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

【问题讨论】:

  • 你试试merge
  • 如果找到匹配则合并否则创建一个新节点并添加关系
  • imgur.com/a/UPxLw请看参考图片

标签: neo4j cypher


【解决方案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)

【讨论】:

  • 嗨@bruno-peres 不幸的是,客户端节点不等于员工列表,所以如果没有可用的 CPR_NUMBER 节点,我们需要以某种方式使用案例(或其他方法)创建一个节点(客户端)
猜你喜欢
  • 2018-07-26
  • 1970-01-01
  • 2022-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-27
  • 2021-12-13
相关资源
最近更新 更多