如何在neo4j中基于子组获得列表输出?

tnkciper  于 2023-10-18  发布在  其他
关注(0)|答案(1)|浏览(124)

我对neo4j很陌生。我们如何分组并以数组的数组形式获得输出。


我需要输出,如[[ST 001,ST 0011,ST 00111],[ST 002,ST 0022,ST 00222],.....,[ST 006,ST 0066,ST 00666]]
先谢谢你的帮助。

ifsvaxew

ifsvaxew1#

一种选择是使用apoc.科尔.排序:

MATCH (n:Node)
WITH n
OPTIONAL MATCH (n)--(m:Node)
WITH COLLECT(m.key) as arr, n.key AS key
WITH arr + key AS arr
WITH apoc.coll.sort(arr) AS arr
RETURN DISTINCT arr

其中:

MERGE (a1:Node {key:'A'}) 
MERGE (a2:Node {key:'B'}) 
MERGE (a3:Node {key:'C'}) 
MERGE (b1:Node {key:'D'}) 
MERGE (b2:Node {key:'E'}) 
MERGE (b3:Node {key:'F'}) 
MERGE (c1:Node {key:'G'})

MERGE (a1)-[:DISTANCE]->(a2)
MERGE (a2)-[:DISTANCE]->(a3)
MERGE (a3)-[:DISTANCE]->(a1)
MERGE (b1)-[:DISTANCE]->(b2)
MERGE (b2)-[:DISTANCE]->(b3)
MERGE (b3)-[:DISTANCE]->(b1)

返回:

╒═══════════════╕
│arr            │
╞═══════════════╡
│["A", "B", "C"]│
├───────────────┤
│["D", "E", "F"]│
├───────────────┤
│["G"]          │
└───────────────┘

如果需要,您还可以添加一个COLLECT

MATCH (n:Node)
WITH n
OPTIONAL MATCH (n)--(m:Node)
WITH COLLECT(m.key) as arr, n.key AS key
WITH arr + key as arr
WITH apoc.coll.sort(arr) AS arr
WITH DISTINCT arr AS arr
RETURN COLLECT(arr) AS arr

获得:

╒═════════════════════════════════════════╕
│arr                                      │
╞═════════════════════════════════════════╡
│[["A", "B", "C"], ["D", "E", "F"], ["G"]]│
└─────────────────────────────────────────┘

相关问题