如果后续MATCH失败,来自WITH的Neo4j值将消失

时间:2014-11-23 10:28:24

标签: neo4j cypher

我遇到了这个问题,我有一个使用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

这很有效。创建KNOWS关系后,connectedtrue其他false。因此,此密码返回truefalse。现在我想扩展这个

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

现在connected仅在标记的匹配成功时才返回truefalse。否则它会给出一个空的结果。无论connected是否成功,我都希望true返回falseMATCH

修改

抱歉,我在第二个查询中遗漏了一些内容。

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

在解决匹配问题时可选匹配,如果出现null错误,则会失败。有什么建议吗?

1 个答案:

答案 0 :(得分:3)

我认为这样的事情可能会成功。

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允许您保留所有初始行,然后您将为&#39; zz&#39;如果没有关系。