title: How do you return all nodes related within 5 hops of the original using Cypher
body: <p>I have a user searching for a node, and I would like to return that node, a set of all the relationships that were found, and a list of the related nodes. I tried something like this...</p>
  
  <pre><code>MATCH (n)
  WHERE n.uuid = &lt;uuid&gt;
  OPTIONAL MATCH n-[r*..5]-(c)
  RETURN distinct(n) as parentNode, collect(r) as links, collect(c) as nodes
  </code></pre>
  
  <p>This however doesn't work because the links come back as a list within a list for some reason. The result from this looks like (using the node module)...</p>
  
  <pre><code>[ //&lt;-- I don't need a list
    {
      "parentNode": {...parent node stuff..},
      "links": [
        [  //&lt;-- This is what I mean by 2 lists
          {...link stuff...}
        ],
        [  //&lt;-- This is what I mean by 2 lists
          {...link stuff...}
        ],
      ],
      "nodes":[
          {...node stuff..}
      ]
    }
  ]
  </code></pre>
  
  <p>I would expect this to look like...</p>
  
  <pre><code>{
      "parentNode": {...parent node stuff..},
      "links": [
        {...link stuff...},
        {...link stuff...},
      ],
      "nodes":[
        {...node stuff..}
      ]
  }
  </code></pre>
  
  <p>If I try a return function that is a little simpler...</p>
  
  <pre><code>RETURN n as parentNode, r as links, c as nodes
  </code></pre>
  
  <p>parent node is repeated for each entry. </p>
  
  <p>How would I properly return this information?</p>
  
  <p><strong>update</strong></p>
  
  <p>So looking a bit more it seems the issue is with the <code>..5</code> this returns 1 set for each node. </p>
  
  <p>So say node 1 is related to node 2 which is related to node 3. And for now say that instead of <code>..5</code> we use <code>..2</code>. The query</p>
  
  <pre><code>MATCH (n)
  WHERE n.number = 1
  OPTIONAL MATCH n-[r*..2]-(c)
  RETURN c as nodes, collect(r) as sublinks
  </code></pre>
  
  <p>Would return something like</p>
  
  <pre><code>[
    {
      "nodes": {..node 2 info...},
      "sublinks":[
         [
            {.. 2 relationships (one to 1 and one to 3)...}
         ]
      ]
    },
    {
      "nodes": {..node 3 info...},
      "sublinks":[
         [
            {.. 1 relationships (one to 2)..}
         ]
      ]
    }
  ]
  </code></pre>
  
  <p>As you can see the relationship between node 2 and 3 is grabbed 2 times. Also node 1 is not returned. These are the types of issues I am trying to avoid.</p>