使用case语句在neo4j cypher中创建关系 [英] create relationships in neo4j cypher using case statement

查看:660
本文介绍了使用case语句在neo4j cypher中创建关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下格式的JSON:

I have a JSON in the next form:

{ "conditions": [ { "id": "123", "type": "a", entities: ["529", "454"] },
                  { "id": "124", "type": "b", entities: ["530", "455"] }
  ]
} 

我想基于type属性(可能是A/B i)创建Condition节点与节点A实体或节点B实体之间的关系,假设这些实体已经存在于neo4j中.

I want to create relation ship between the Condition node with node A Entities or node B entities based on type attribute which can be A/B i Assuming that these entities already exists in neo4j.

我正在尝试类似下面的密码的方法,但这是行不通的.

I am trying something like the below cypher but that doesn't work.

WITH {json} as data
UNWIND data.conditions as condition 
MATCH (c:Condition { conditionId: condition.id})
CASE c.type 
    WHEN 'a' THEN FOREACH (sid IN condition.entities | 
        MERGE (s:NodeA {nr_serverId:sid}) MERGE (s)-[:ATTACHED_TO]->(c)
    )
    WHEN 'b' THEN FOREACH (aid IN condition.entities | 
       MERGE (a:NodeB {nr_appId: aid}) MERGE (a)-[:ATTACHED_TO]->(c)
    )
END;

任何人都可以通过正确的方法来帮助我吗?谢谢.

Can anyone please help me with the correct way of doing this? Thank you.

推荐答案

由于cypher中目前没有经典的条件语句,因此您可以使用

Since at the moment there is no classical conditional statements in cypher, you can use the famous trick with foreach and case:

WITH {json} as data
UNWIND data.conditions as condition 
MATCH (c:Condition { conditionId: condition.id})
FOREACH (ift in CASE WHEN c.type = 'a' THEN [1] ELSE [] END |
    FOREACH (sid IN condition.entities | 
        MERGE (s:NodeA {nr_serverId:sid}) MERGE (s)-[:ATTACHED_TO]->(c)
    )
)
FOREACH (ift in CASE WHEN c.type = 'b' THEN [1] ELSE [] END |
    FOREACH (aid IN condition.entities | 
       MERGE (a:NodeB {nr_appId: aid}) MERGE (a)-[:ATTACHED_TO]->(c)
    )
)

这篇关于使用case语句在neo4j cypher中创建关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆