Given the below cypher query
MATCH (u:User {id: {userId}}), (b:B {id: {bId})
CREATE (c:C), (c)-[:HAS_USER]->(u), (b)-[:SOME_REL]->(c)
As you can see, I'm creating a node C
that must have relationships with 2 things, some node b
and some user.
What happens is I get an empty array when u or b does not exist, but I'd like neo4j to respond with a fail instead of an empty array. That makes things easier for me to know which node is missing. Is it possible to 'force' a fail when the match clause doesn't return anything?
That is how it works, if the MATCH
return null then the query fails. That is why they have OPTIONAL MATCH
available so that it doesn`t fail if null is returned.
Edit: add return at the end like this
MATCH (u:User {id: {userId}}), (b:B {id: {bId})
CREATE (c:C), (c)-[:HAS_USER]->(u), (b)-[:SOME_REL]->(c)
RETURN 'success'
So if you get success back that means that match found what it was looking for if not then it didn't
edit 2:
OPTIONAL MATCH (u:User {id: {userId}}), (b:B {id: {bId})
with *,CASE when u is not null and b is not null then [1] else [] end as exists
FOREACH (x in exists | CREATE (c:C), (c)-[:HAS_USER]->(u), (b)-[:SOME_REL]->(c))
RETURN u,b
so now we do an optional match so it doesnt break down when not found. Then we do a CASE statement and where both User and B exist we create some relationships. And in the end we return User and b and check if both exists or whether there is a null in any of them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With