需要在C#中的Neo4j数据格式JSON。

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

Need Neo4j Data Format json in c#

问题

以下是您要的内容的翻译:

{
    "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": []
}
英文:

I'm trying to find the shortest path along with relations on the nodes on the path, for which below query is used.

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

However i want to return the result as json object with data format as below in c#. I understand we have to use apoc. However can't really understand how to proceed.

{
"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": []

}

答案1

得分: 1

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

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
英文:

You can collect the nodes and relationships separately and add it on the result.

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

答案2

得分: 1

抱歉,你提供的内容已经是代码,不需要翻译。

英文:

I wanted the first level incoming and outgoing relations of all the nodes on the path. Slighty modified the answer for anyone looking for something similar in future. Thanks 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

huangapple
  • 本文由 发表于 2023年2月16日 14:06:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468426.html
匿名

发表评论

匿名网友

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

确定