1

我为此手动使用 cypher rest api。

我想以易于解析的方式返回数据。

这是我正在处理的具有相同关系的一些数据的示例:

(me:Person:Normal),
(dad:Person:Geezer),
(brother:Person:Punk),
(niece:Person:Tolerable),
(daughter:Person:Awesome),
(candy:Rule),
(tv:Rule),
(dad)-[:HAS_CHILD {Num:1}]->(brother),
(dad)-[:HAS_CHILD {Num:2}]->(me),
(me)-[:HAS_CHILD {Num:1}]->(daughter),
(brother)-[:HAS_CHILD {Num:1}]->(niece),
(me)-[:ALLOWS]->(candy),
(me)-[:ALLOWS]->(tv)

我想获得所有的 HAS_CHILD 关系,如果这些子节点中的任何一个具有 :ALLOWS 关系,我也想要这些关系的 id。

所以如果我要做类似的事情......

START n=node({idofdad}) MATCH n-[r?:HAS_CHILD]->h-[?:ALLOWS]->allowed
    WITH n, r, h, collect(ID(allowed)) as allowedIds
    WITH n, r, 
        CASE 
            WHEN h IS NOT NULL THEN [ID(h), LABELS(h), r.Num?, allowedIds] 
            ELSE NULL     
        END as has
RETURN
    LABELS(n) as labels,
    ID(n) as id,
    n as node,
    COLLECT(has) as children;

:HAS_CHILD 可能不存在,所以我必须做这个奇怪的事情。

返回的数据是“好的”,但我拥有的 JSON 映射器(Newtonsoft)并不容易将数组映射到对象(这意味着我知道数组索引 [0] 是(子)集合的 ID .

上面的结果如下所示: { "columns": ["labels", "id", "node", "children"], "data": [ ["Person", "Geezer"], 6, { }, [ [7, ["Person", "Normal"], 2, [2, 1]], [5, ["Person", "Punk"], 1, []] ] ] }

由于这或多/少是一个文档,并且映射“儿童”列会更容易,我希望得到如下所示的内容:

{
  "columns": ["labels", "id", "node", "children"], 
  "data": [
    ["Person", "Geezer"],
    6,
    {},
    [
     [
       "id": 7, 
       "labels": ["Person", "Normal"], 
       "ChildNumber": 2, 
       "AllowedIds": [2, 1]
     },
     {
       "id": 5, 
       "labels": ["Person", "Punk"], 
       "ChildNumber": 1, 
       "AllowedIds": []
     }
    ]
  ] 
}

我希望查询看起来像:

START n=node({idofdad}) MATCH n-[r?:HAS_CHILD]->h-[?:ALLOWS]->allowed
    WITH n, r, h, collect(ID(allowed)) as allowedIds
    WITH n, r, 
        CASE 
            WHEN h IS NOT NULL THEN 
               { id: ID(h), labels: LABELS(h), ChildNumber: r.Num?, AllowedIds: allowedIds }
            ELSE NULL     
        END as has
RETURN
    LABELS(n) as labels,
    ID(n) as id,
    n as node,
    COLLECT(has) as children;

这甚至有可能吗?

4

0 回答 0