Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Neo4j 根据条件创建节点_Neo4j_Cypher - Fatal编程技术网

Neo4j 根据条件创建节点

Neo4j 根据条件创建节点,neo4j,cypher,Neo4j,Cypher,我试图在Neo4j中建立节点之间的关系,如果满足某个条件,如。目前我有节点(a)和节点(b): 我想要什么 if node(b) is in label1 then make relation: node(a)-[:r]-node(b:label1) else merge node(b) in label2 then make relation node(a)-[:r]-node(b:label2) 我所拥有的 match (a:label1 {id:"t1"}) merge (b:labe

我试图在Neo4j中建立节点之间的关系,如果满足某个条件,如。目前我有节点(a)和节点(b):

我想要什么

if node(b) is in label1 then make relation: node(a)-[:r]-node(b:label1)
else merge node(b) in label2 then make relation node(a)-[:r]-node(b:label2)
我所拥有的

match (a:label1 {id:"t1"}) 
merge (b:label1 {id:"t6"})
on create 
  set b:label2 remove b:label1 
merge (a)-[:Friends_with]-(b)

不幸的是,Cypher实际上没有if-then-else语法(在匹配和创建时是最接近的)。我建议运行多个密码,并根据返回结果执行后续操作

很像

MATCH (a:label1 {id:"t1"}) 
MATCH (b:label1 {id:"t6"})
MERGE (a)-[:Friends_with]-(b)
return COUNT(b) as b1Exists
如果返回0,则执行

MATCH (a:label1 {id:"t1"}) 
MERGE (b:label2 {id:"t6"})
MERGE (a)-[:Friends_with]-(b)
根据您的数据,您可能会侥幸逃脱

MATCH (a:label1 {id:"t1"}) 
MERGE (b {id:"t6"})
ON CREATE SET b:label2
MERGE (a)-[:Friends_with]-(b)

但要知道,在1个密码中执行此操作可能会导致错误(在这种情况下,如果有效标签多于1和2,则需要此方法的UUID)

编辑窗口中的格式化代码。查看编辑窗口顶部的{}图标。到目前为止,您能分享您的查询吗?在创建集nodeb:label2删除nodeb:label1合并(nodea)-[:Friends_with]-(nodeb)上匹配(nodea:label1{id:“t1”})合并(nodeb:label1{id:“t6”})