Neo4j / Cypher - 返回带有链接节点的嵌套JSON对象

时间:2018-03-14 14:39:33

标签: json neo4j cypher

我有家长评论和孩子评论。我希望能够将我的子注释组合在一起作为我父对象的一个​​对象。这是我的Neo4j代码:

MATCH (c:Comment)<-[:COMMENTED_ON]-(cc:Comment)
WHERE c.GUID=$GUID
RETURN collect({comment: c, subcomment: cc})

这是我收到的JSON输出:

[
    {
        "parentComment": {
            "identity": {
                "low": 6418,
                "high": 0
            },
            "labels": [
                "Action",
                "Comment"
            ],
            "properties": {
                "GUID": "77750e90-77cf-4e40-9753-87ba828366a5",
                "text": "This is a comment",
                "type": "Comment",
                "timestamp": 20171220111906416
            }
        },
        "childComment": {
            "identity": {
                "low": 8404,
                "high": 0
            },
            "labels": [
                "Comment"
            ],
            "properties": {
                "user": "James",
                "text": "Test comment",
                "timestamp": 20180314142848576
            }
        }
    },
    {
        "parentComment": {
            "identity": {
                "low": 6418,
                "high": 0
            },
            "labels": [
                "Action",
                "Comment"
            ],
            "properties": {
                "GUID": "77750e90-77cf-4e40-9753-87ba828366a5",
                "text": "This is a comment",
                "type": "Comment",
                "timestamp": 20171220111906416
            }
        },
        "childComment": {
            "identity": {
                "low": 8659,
                "high": 0
            },
            "labels": [
                "Comment"
            ],
            "properties": {
                "user": "James S",
                "text": "TEST",
                "timestamp": "today"
            }
        }
    }
]

正如您所看到的那样,每次都会使用不同的孩子两次打印父评论。我需要在Cypher查询中包含什么才能将子节点组合在一起?

1 个答案:

答案 0 :(得分:2)

您希望以这种方式返回地图投影:

MATCH (c:Comment)<-[:COMMENTED_ON]-(cc:Comment)
WHERE c.GUID=$GUID
RETURN {comment : c, subcomments: collect(cc)}