在Neo4j中检索节点列表以及与它们直接相关的节点的ID列表

时间:2016-04-24 14:02:53

标签: node.js neo4j cypher

在我的数据库中,我有:用户节点,它们通过以下方式相关:友谊关系。我想得到这样的结构:

[
    {
        id: 1,
        username: "Whatever",
        email: "whatever@test.com"
        ...
    },
    [ 6, 7, 8, ... ]
],
[
    {
        id: 2,
        username: "Another user",
        email: "anotheruser@test.com"
        ...
    },
    [ 15, 16, 17, 18, ... ]
],
...

...其中数字是节点与a:友谊关系直接相关的节点的ID。

这个答案有一些几乎完成工作的查询:

Can I find all the relations between two nodes in neo4j?

但我提出的最接近的是:

match p=(a:User)-[:Friendship]->(d:User)
return d, reduce(nodes = [],n in nodes(p) | nodes + [id(n)]) as node_id_col

...返回此结构:

[
    {
        id: 1,
        username: "Whatever",
        email: "whatever@test.com"
        ...
    },
    [ 1, 6 ]
],
[
    {
        id: 1,
        username: "Whatever",
        email: "whatever@test.com"
        ...
    },
    [ 1, 7 ]
],
[
    {
        id: 1,
        username: "Whatever",
        email: "whatever@test.com"
        ...
    },
    [ 1, 8 ]
],
[
    {
        id: 2,
        username: "Another user",
        email: "anotheruser@test.com"
        ...
    },
    [ 2, 15 ]
],
[
    {
        id: 2,
        username: "Another user",
        email: "anotheruser@test.com"
        ...
    },
    [ 2, 16 ]
],
...

这不好,因为它返回了大量冗余数据。

那么Cypher对此的查询是什么?

谢谢!

1 个答案:

答案 0 :(得分:2)

我认为你可能会使事情变得复杂,或者我没有正确理解这个问题。这样的事情对你有用吗?

match (a:User)-[:Friendship]->(d:User)
return a, collect(id(d))