需要Neo4j数据格式json在C#

2mbi3lxu  于 2023-02-19  发布在  C#
关注(0)|答案(2)|浏览(161)

我试图找到最短路径以及路径上节点的关系,为此使用了下面的查询。

MATCH p = shortestPath((p1:Person { name: 'Kevin Bacon' })-[*..15]- 
  (p2:Person { name: 'Meg Ryan' })) 
  UNWIND nodes(p) as n 
  MATCH (n)-[*]->(q) 
  RETURN n, q

然而,我想返回的结果作为json对象与数据格式如下在c#。我知道我们必须使用apoc。但不能真正理解如何进行。

{
"results": [
    {    
        "data": [
            {
                "graph": {
                    "nodes": [
                        {
                            "id": "1",
                            "labels": ["James"],
                            "properties": {
                                "ShortName": "jammy",
                                "Type": "Person",
                                "Age": 34
                            }
                        },
                        {
                            "id": "2",
                            "labels": ["Brad"],
                            "properties": {
                                "name": "Brad",
                                "PlaceOfBirth": "California",
                                 "Type": "Person",
                                "description": "Nice actor",
                            }
                        },
                        {
                            "id": "3",
                            "labels": ["Titanic"],
                            "properties": {
                                "movieName": "Titanic", 
                                 "Type": "Movie",         
                                "description": "Tragedy",
                            }
                        }
                    ],
                    "relationships": [
                        {
                            "id": "4",
                            "type": "ACTED_IN",
                            "startNode": "1",
                            "endNode": "3",
                            "properties": {
                                "from": 1470002400000
                            }
                        }
                    ]
                }
            }
        ]
    }
],
"errors": []

}

hgtggwj0

hgtggwj01#

您可以分别收集节点和关系并将其添加到结果中。

MATCH p = shortestPath((p1:Person { name: 'Kevin Bacon' })-[*..15]-(p2:Person { name: 'Meg Ryan' })) 
  UNWIND nodes(p) as n 
  MATCH (n)-[r]->(q) 
  WITH collect(distinct n) + collect(distinct q) as node_list, collect(distinct r) as rel_list
  RETURN {results: {data: {graph: {nodes: node_list, relationships: rel_list}}, error: []}} as output
628mspwn

628mspwn2#

我想要路径上所有节点的第一层传入和传出关系。Slighty修改了答案,以供将来寻找类似内容的人使用。谢谢Jose。

MATCH p = shortestpath((p1:Person { name: 'Kevin Bacon' })-[*..30]-   
(p2:Person { name: 'Meg Ryan' })) 
UNWIND nodes(p) as n 
MATCH (n)<-[r*1]->(q) 
WITH collect(distinct n) + collect(distinct q) as node_list, 
collect(distinct r) as rel_list
RETURN {results: {data: {graph: {nodes: node_list, relationships: 
rel_list}}, error: []}} as output

相关问题