如何在Cypher中为每个节点返回相关节点的列表

huangapple go评论51阅读模式
英文:

How to return a list of related nodes for each node in Cypher

问题

我想为下面的图编写一个查询,该查询应返回三个列表,每个列表包含所有为同一人工作的人员。

给定下面的图,结果应该是这三个列表:

  • [Fritz]
  • [Pepe]
  • [Susy, Peter]

以下是为我提供的示例创建图模型的语句:

MATCH (c:Example) DETACH DELETE c;

CREATE (p1:Parent:Example {id: 1, name: 'Andy', title: 'Developer'});
CREATE (p2:Parent:Example {id: 2, name: 'Lila', title: 'Developer'});
CREATE (p3:Parent:Example {id: 3, name: 'Lula', title: 'Developer'});

CREATE (c11:Child:Example {id: 11, name: 'Peter', title: 'Developer'});
CREATE (c12:Child:Example {id: 12, name: 'Susy', title: 'Developer'});

CREATE (c21:Child:Example {id: 21, name: 'Fritz', title: 'Developer'});

CREATE (c31:Child:Example {id: 31, name: 'Pepe', title: 'Developer'});

MATCH (p {id: 1}), (c {id: 11}) MERGE (p)<-[:WORKS_FOR]-(c);
MATCH (p {id: 1}), (c {id: 12}) MERGE (p)<-[:WORKS_FOR]-(c);
MATCH (p {id: 2}), (c {id: 21}) MERGE (p)<-[:WORKS_FOR]-(c);
MATCH (p {id: 3}), (c {id: 31}) MERGE (p)<-[:WORKS_FOR]-(c);

你的查询返回了所有雇员在一个列表中,而不是你期望的三个列表。

英文:

I would like to write a query for the graph below, which should return three lists, each list containing all persons working for the same person.

Given the graph below, the result should be this three lists:

  • [Fritz]
  • [Pepe]
  • [Susy, Peter]

如何在Cypher中为每个节点返回相关节点的列表

I fail to write such query. My query returns all employes in one list.

The following statements create the graph model for the example I have given:

MATCH  (c:Example) DETACH DELETE c;

CREATE (p1:Parent:Example {id: 1, name: &#39;Andy&#39;, title: &#39;Developer&#39;});
CREATE (p2:Parent:Example {id: 2, name: &#39;Lila&#39;, title: &#39;Developer&#39;});
CREATE (p3:Parent:Example {id: 3, name: &#39;Lula&#39;, title: &#39;Developer&#39;});

CREATE (c11:Child:Example {id: 11, name: &#39;Peter&#39;, title: &#39;Developer&#39;});
CREATE (c12:Child:Example {id: 12, name: &#39;Susy&#39;, title: &#39;Developer&#39;});

CREATE (c21:Child:Example {id: 21, name: &#39;Fritz&#39;, title: &#39;Developer&#39;});

CREATE (c31:Child:Example {id: 31, name: &#39;Pepe&#39;, title: &#39;Developer&#39;});

MATCH (p {id: 1}), (c {id: 11}) MERGE (p)&lt;-[:WORKS_FOR]-(c);
MATCH (p {id: 1}), (c {id: 12}) MERGE (p)&lt;-[:WORKS_FOR]-(c);
MATCH (p {id: 2}), (c {id: 21}) MERGE (p)&lt;-[:WORKS_FOR]-(c);
MATCH (p {id: 3}), (c {id: 31}) MERGE (p)&lt;-[:WORKS_FOR]-(c);

答案1

得分: 2

这里使用Cypher相对简单

MATCH (c)-[:WORKS_FOR]->(p)
RETURN p.name AS boss, collect(c.name) AS coWorkers


结果

╒══════╤════════════════╕
│"boss"│"coWorkers" │
╞══════╪════════════════╡
│"Andy"│["Peter","Susy"]│
├──────┼────────────────┤
│"Lila"│["Fritz"] │
├──────┼────────────────┤
│"Lula"│["Pepe"] │
└──────┴────────────────┘


关键是理解聚合 https://neo4j.com/docs/cypher-manual/current/functions/aggregating/#grouping-keys
英文:

It's relatively straightforward with Cypher

MATCH (c)-[:WORKS_FOR]-&gt;(p)
RETURN p.name AS boss, collect(c.name) AS coWorkers

Result

╒══════╤════════════════╕
│&quot;boss&quot;│&quot;coWorkers&quot;     │
╞══════╪════════════════╡
│&quot;Andy&quot;│[&quot;Peter&quot;,&quot;Susy&quot;]│
├──────┼────────────────┤
│&quot;Lila&quot;│[&quot;Fritz&quot;]       │
├──────┼────────────────┤
│&quot;Lula&quot;│[&quot;Pepe&quot;]        │
└──────┴────────────────┘

The trick is understanding aggregations https://neo4j.com/docs/cypher-manual/current/functions/aggregating/#grouping-keys

huangapple
  • 本文由 发表于 2023年2月6日 03:46:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355043.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定