在未找到匹配项的可选匹配之后创建 [英] CREATE after OPTIONAL MATCH where no match was found
问题描述
我正在尝试编写一个查询,该查询将在已经存在另一个关系的情况下创建一些关系.
I'm trying to write a query that will create some relationships if another relationship already exists.
START a=node(1), b=node(2), c=node(3)
OPTIONAL MATCH a-[r1:RELATIONSHIP]-(optional1)
OPTIONAL MATCH b-[r2:RELATIONSHIP]-(optional2)
CREATE c-[:NEW_RELATIONSHIP]->(optional1)
CREATE c-[:NEW_RELATIONSHIP]->(optional2)
DELETE r1, r2
RETURN a, b, c
查询返回错误:
将optional1
预期为节点,但它为null"
"Expected optional1
to a node, but it is a null"
如果现有关系存在,是否可以创建新关系?否则就忽略创建吗?
Is there a way to create the new relationship if the existing relationship exists? Otherwise just ignore the create?
推荐答案
现在,您可以使用FOREACH
和CASE
解决此问题.例如:
For now, you can work around this using FOREACH
and CASE
. For example:
START a=node(1), b=node(2), c=node(3)
OPTIONAL MATCH (a)-[r1:RELATIONSHIP]-(optional1)
OPTIONAL MATCH (b)-[r2:RELATIONSHIP]-(optional2)
FOREACH (o IN CASE WHEN optional1 IS NOT NULL THEN [optional1] ELSE [] END |
CREATE (c)-[:NEW_RELATIONSHIP]->(optional1)
)
FOREACH (o IN CASE WHEN optional2 IS NOT NULL THEN [optional2] ELSE [] END |
CREATE (c)-[:NEW_RELATIONSHIP]->(optional2)
)
DELETE r1, r2
RETURN a, b, c
我怀疑在将来的Cypher语言更新中这将变得更加简单.
I suspect this will be made simpler in future Cypher language updates.
PS.您可能不应该再使用START
了,但是我想您只是出于示例方便.
PS. You probably shouldn't use START
anymore, but I imagine you have simply for the convenience of an example.
这篇关于在未找到匹配项的可选匹配之后创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!