Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Neo4j 规范化查询结果_Neo4j_Cypher - Fatal编程技术网

Neo4j 规范化查询结果

Neo4j 规范化查询结果,neo4j,cypher,Neo4j,Cypher,我有一个设置,其中多个1节点连接到多个节点 所以,当我获取一些路径时,那个些连接到多个节点的1个节点会在查询结果中重复 MATCH (r:Red) OPTIONAL MATCH (r:Red)-[rel]-(g:Grey) RETURN ID(r), g.prop, rel.prop 返回 ID(r) g.prop rel.prop 1131 null null >1132 b value1 >1132 b2 value2 >

我有一个设置,其中多个1节点连接到多个节点

所以,当我获取一些路径时,那个些连接到多个节点的1个节点会在查询结果中重复

MATCH (r:Red)
OPTIONAL MATCH (r:Red)-[rel]-(g:Grey)
RETURN ID(r), g.prop, rel.prop
返回

ID(r)   g.prop  rel.prop
1131    null    null
>1132   b       value1
>1132   b2      value2
>1132   b3      value3
1134    c       value4
然后我将其转换为可用的javascript数组/对象

results.forEach(function(result){
    Red.push({
        red: result['ID(r)'],
        Grey: {
            greyProp: result['g.prop'],
            greyRelation: result['r.prop']
        }
    });
});
产生

Red:
[
{"red":1131,"Grey":{"greyProp":null,"greyRelation":null}}
,
{"red":1132,"Grey":{"greyProp":"b","greyRelation":"value1"}}
,
{"red":1132,"Grey":{"greyProp":"b2","greyRelation":"value2"}}
,
{"red":1132,"Grey":{"greyProp":"b3","greyRelation":"value3"}}
,
{"red":1134,"Grey":{"greyProp":"c","greyRelation":"value4"}}
]
但我宁愿把它当作

Red:
[
{"red":1131,"Grey":{"greyProp":null,"greyRelation":null}}
,
{"red":1132,"Grey":
    [
        {"greyProp":"b","greyRelation":"value1"},
        {"greyProp":"b2","greyRelation":"value2"},
        {"greyProp":"b3","greyRelation":"value3"}
    ]
,
{"red":1134,"Grey":{"greyProp":"c","greyRelation":"value4"}}
]

我想不出一个办法。根据第一次查询的结果生成对象时,是否需要再次运行查询?或者有没有一种更简单、更简单的方法可以做到这一点?

您可能想这样做

MATCH (r:Red)
OPTIONAL MATCH (r:Red)-[rel]-(g:Grey)
RETURN ID(r), collect(g.prop) as g_prop, collect(rel.prop) as rel_prop
这将给你类似的结果

ID(r)   g_prop        rel_prop
1131    null          null
1132    [b,b2,b3]     [value1,value2,value3]
1134    c             value4
ID(r)   g_prop        rel_prop
1131    null          null
1132    [b,b2,b3]     [value1,'null',value3]
1134    c             value4
**根据LaggingReflection指出的缺点。修改后的查询将在集合中找不到rel prop的位置放置“NULL”字符串。(假设所有
g.prop
都是
never NULL
,如果是,则同样使用
case..when
collect(g.prop)
中)

这将给你类似的结果

ID(r)   g_prop        rel_prop
1131    null          null
1132    [b,b2,b3]     [value1,value2,value3]
1134    c             value4
ID(r)   g_prop        rel_prop
1131    null          null
1132    [b,b2,b3]     [value1,'null',value3]
1134    c             value4
一个(主要)缺点是
rel\u prop
完全独立于
g\u prop
。i、 e.我能够重现
1132[b,b2,b3][value1,value3]
(通过删除
value2
,这是b2的内部依赖关系)