问题
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