# Get all nodes connected

**URL:** https://community.neo4j.com/t/get-all-nodes-connected/55997
**Category:** Neo4j Graph Platform
**Tags:** cypher
**Created:** [May 8, 2022, 3:33pm UTC](https://community.neo4j.com/t/get-all-nodes-connected/55997 "2022-05-08T15:33:17Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![vence\_andersen1](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/vence_andersen1/32/19186_2.png) [@vence\_andersen1](https://community.neo4j.com/u/vence_andersen1)
#### Post date: [May 8, 2022, 3:33pm UTC](https://community.neo4j.com/t/get-all-nodes-connected/55997/1 "2022-05-08T15:33:17Z")

</div>

![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/4/c/4c22619ce8d92d7f2b704707bcb738f07cb838cb.png)

I have nodes like the above image, now can I get all nodes from A1 to A7 by querying A1 or getting all nodes from B1 to B5 by querying B1. Here we don't know the number of nodes connected, as you can see A1 has 6 nodes, B2 has nodes through it's path. All the nodes are having the same label, so we can't fetch them with the label.

Thanks in advance.

---

<div class="post-metadata">

### Author: ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)
#### Post date: [May 8, 2022, 9:27pm UTC](https://community.neo4j.com/t/get-all-nodes-connected/55997/2 "2022-05-08T21:27:36Z")

</div>

You would use a pattern that matches your desired result. You can add as many constrains to the pattern to restrict your result to exactly what you need. In your cases, you basically have an anchor node you can explicitly identify (A1, B1, or C1) and a path consisting of a variable number of hops. Assuming you don't care about the relationship type, the following should work. I have just identified the anchor node by its id. Substitute your current criteria for selecting the anchor node. I also left off specific node labels. Add those as needed as well.

```auto
match (n where id(n) = 1)
match p = (n)-[*]->()
return p

```

'p' is a path. it consists of a collection of nodes and a collection of relationships that comprise the path. You can return them instead with the following query:

```auto
match (n where id(n) = 1)
match p = (n)-[*]->()
return nodes(p) as nodes, relationships(p) as relationships

```

nodes and relationships will be lists of those entities. Each relationship consists of its starting node, ending node, and relationship properties. For a given relationship r, its starting node is startNode(r), its ending node is endNode(r), and its properties is properties(r).

---

<div class="post-metadata">

### Author: ![ameyasoft](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ameyasoft/32/32639_2.png) [@ameyasoft](https://community.neo4j.com/u/ameyasoft)
#### Post date: [May 9, 2022, 4:05am UTC](https://community.neo4j.com/t/get-all-nodes-connected/55997/3 "2022-05-09T04:05:38Z")

</div>

```auto
Try this:

MATCH (a:Test) where a.name = "A1"  
CALL apoc.path.spanningTree(a,{}) YIELD path  

with nodes(path) as n1
 unwind n1 as a1
 with a1

 MATCH (a1:Test) where a1.name = "B1"  
CALL apoc.path.spanningTree(a1,{}) YIELD path  

with a1, nodes(path) as n2
 unwind n2 as b1
 with a1, b1 

MATCH (a2:Test) where a1.name = "C1"  
CALL apoc.path.spanningTree(a2,{}) YIELD path  

with a1, b1, nodes(path) as n3
unwind n3 as c1
with a1, b1, c1 

return a1, b1, c1

```

---

<div class="post-metadata">

### Author: ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)
#### Post date: [May 9, 2022, 6:34am UTC](https://community.neo4j.com/t/get-all-nodes-connected/55997/4 "2022-05-09T06:34:45Z")

</div>

Hello @vence_andersen1 🙂

The best solution is to use GDS and the [Weakly Connected Components](https://neo4j.com/docs/graph-data-science/current/algorithms/wcc/) algorithm.

Some examples:

- [How to get group of connected nodes?](https://community.neo4j.com/t/how-to-get-group-of-connected-nodes/41855/4)
- [How to remove connected components less than x nodes?](https://community.neo4j.com/t/how-to-remove-connected-components-less-than-x-nodes/24166/73)
- [Delete a subgraph from a database](https://community.neo4j.com/t/delete-a-subgraph-from-a-database/55446/8)

Regards,  
Cobra
