在 neo4j 密码中找到一个节点及其所有传入和传出节点

Find a node and all its incoming and outgoing nodes in neo4j cypher

我想编写一个密码查询来获取节点及其所有传入和传出关系。

例如,假设我们有节点 N,有两个传入关系:(I1)-[IR1]->(N) 和 (I2)-[IR2]->(N),以及两个传出关系: (N)-[OR1]->(O1) 和 (N)-[OR2]->(O2).

我想要一个可以产生的查询:

{
    node: { properties of N },
    incoming: [
        { relationship: IR1, node: { properties of I1 } },
        { relationship: IR2, node: { properties of I2 } }
    ],
    outgoing: [
        { relationship: OR1, node: { properties of O1 } },
        { relationship: OR2, node: { properties of O2 } }
    ]
}

我能得到的最接近的密码查询是:

match (node { criterial })
match (incoming)-[incomingr]->(node)
match (node)-[outgoingr]->(outgoing)
return node, collect(distinct incoming), collect(distinct outgoing)

但不包含type(incomingr)和type(outgoingr)。

返回路径也没有给我想要的东西,因为它包括关系属性但不包括类型,更不用说它了 returns (node) 的许多重复副本。

我知道我可以

return node, incoming, outgoing, type(incomingr), type(outgoingr)

获取所有内容,然后通过 JSON 处理以获得我想要的内容,但是随着关系数量的增加,返回的数据将变得太大,因为它 returns 的组合所有传入路径和传出路径。 它就是不整洁。

基于 http://console.neo4j.org 中的标准演示数据集,以下查询接近您想要实现的结果:

MATCH (n:Crew)
WHERE n.name='Morpheus'
MATCH ()-[rin]->(n)-[rout]->()
WITH n, collect(DISTINCT 
       { relationship:type(rin), 
         node: startNode(rin)
       }) AS incoming, 
collect(DISTINCT 
       { relationship:type(rout), 
         node: endNode(rout)
       }) AS outgoing
RETURN { node: n, incoming: incoming, outgoing: outgoing } AS result