create nodes depending on certain condition in Neo4j

故事扮演 提交于 2019-12-12 03:34:00

问题


I want to create some new nodes in my database if some condition is satisfy

MATCH (u:User)-[:has]->(a:Account)-[:initiated]->(l:Process) WHERE ID(l)=984 
UNWIND [{mobile:123,email:'a@b.com'}, {mobile:456, email:'a1@b1.com'}] as x
OPTIONAL MATCH (u1:User) WHERE u1.mobile = x.mobile OR u1.email = x.email CASE u1 WHEN u1 IS NULL THEN CREATE (u)-[:pending]->(p:Pending {mobile: x.mobile, email: x.email})
ELSE CREATE (u1)-[:pending]->(p:Pending {mobile: x.mobile, email: x.email})
END 

I want to check condition whether any users exist with mobile or email. If exist I want to create node (p) attached to there node (u1) else I want to create node attached to my node i.e (u).

Somehow create is not working in case


回答1:


Currently the only way to do conditional writes is using the FOREACH/CASE WHEN trick. Based on your condition you create either a 1 element or an empty array and iterate over that using FOREACH, e.g.

...
FOREACH(x in CASE WHEN u1 IS null THEN [1] ELSE [] END | 
  CREATE (u)-[:pending]->(p:Pending {mobile: x.mobile, email: x.email}))
FOREACH(x in CASE WHEN u1 IS NOT null THEN [1] ELSE [] END | 
  CREATE (u1)-[:pending]->(p:Pending {mobile: x.mobile, email: x.email}))

See http://www.markhneedham.com/blog/2014/06/17/neo4j-load-csv-handling-conditionals/ for more details.



来源:https://stackoverflow.com/questions/35453151/create-nodes-depending-on-certain-condition-in-neo4j

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!