Reputation: 3215
I ran into this issue I have a cypher that passes on data using WITH
MATCH (u1:user {username: "pewpewlasers"}), (u2:user {username: "pewpew2"})
MERGE (u1)-[y:KNOWS]->(u2)
ON CREATE SET y.connected = 1
WITH y, has(y.connected) AS connected
REMOVE y.connected
RETURN connected
This works. When KNOWS
relationship is created, connected
is true
else false
. And so either true
or false
is returned by this cypher. Now I want to extend this
MATCH (u1:user {username: "pewpewlasers"}), (u2:user {username: "pewpew2"})
MERGE (u1)-[y:KNOWS]->(u2)
ON CREATE SET y.connected = 1
WITH y, has(y.connected) AS connected
REMOVE y.connected
WITH y, connected
MATCH (x)-[zz:LIKES]->(y) <------------- this match
WITH zz, connected
RETURN zz, connected
NOW connected
returns true
or false
only if the marked match succeeds. Otherwise it gives an empty result. I want connected
to return true
or false
regardless of whether MATCH
succeeds or not.
EDIT:
Sorry I left out something in the second query.
MATCH (u1:user {username: "pewpewlasers"}), (u2:user {username: "pewpew2"})
MERGE (u1)-[y:KNOWS]->(u2)
ON CREATE SET y.connected = 1
WITH y, has(y.connected) AS connected
REMOVE y.connected
WITH y, connected
MATCH (x)-[zz:LIKES]->(y) <------------- this match
MERGE x-[:LOVES]->(y) <------------- EDIT
WITH zz, connected
RETURN zz, connected
Optional match while solves the match issue, it will fail with merge giving null
error. Any suggestions?
Upvotes: 1
Views: 150
Reputation: 2128
I think something like this may do the trick.
MATCH (u1:user {username: "pewpewlasers"}), (u2:user {username: "pewpew2"})
MERGE (u1)-[y:KNOWS]->(u2)
ON CREATE SET y.connected = 1
WITH y, has(y.connected) AS connected
REMOVE y.connected
WITH y, connected
OPTIONAL MATCH (x)-[zz:LIKES]->(yy)
FOREACH(ignoreMe IN CASE WHEN yy IS null THEN [] ELSE [1] END |
MERGE x-[:LOVES]->(yy))
WITH zz, connected
RETURN zz, connected
OPTIONAL MATCH allows you to keep all your initial rows and then you'll have a null value for 'zz' if there is no relationship.
Upvotes: 3