2
votes

I have four nodes that -[belongTo]-> (ContainerNode) I want the json to return as a single container node which contains an array of all the nodes that link to it. For example:

 "nodes": [
      {
        "id": "240",
        "name":"MyNodeContainer",
        "Type": "ContainerNode"
        "SubNodes": [
                   {
                     "id": "1",
                    "name":"MyNodeA",
                    "Type": "node"
                   },
                   {
                     "id": "2",
                    "name":"MyNodeB",
                    "Type": "node"
                   }
                 ]
      },

It seems simple but all i can get is the default of all nodes being returned as equal. I want the result to make it clear that the container node is separate from the rest. An array property seems most intuitive but i would also be content with two lists - one for the single nodeContainer and one for the contained nodes

1

1 Answers

1
votes

Does something like this steer you towards your end goal? It builds a collection of the contained nodes and then returns it as a property of the ContainerNode.

match (c:ContainerNode)<-[:BELONGS_TO]-(n:Node)
with c, collect({ id: id(n), name: n.name, type: labels(n)[0] }) as nodes
with { id: id(c), name: c.name, type: labels(c)[0], SubNodes: nodes } as containerNode
return {nodes: collect(containerNode) }