# Get directly connected nodes for multiple nodes in one query?

**URL:** <https://community.neo4j.com/t/get-directly-connected-nodes-for-multiple-nodes-in-one-query/50878>\
**Category:** Cypher\
**Created:** [January 22, 2022, 7:57pm UTC](https://community.neo4j.com/t/get-directly-connected-nodes-for-multiple-nodes-in-one-query/50878 "2022-01-22T19:57:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![arielb135](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/arielb135/32/22027_2.png) [@arielb135](https://community.neo4j.com/u/arielb135)\
**Post date:** [January 22, 2022, 7:57pm UTC](https://community.neo4j.com/t/get-directly-connected-nodes-for-multiple-nodes-in-one-query/50878/1 "2022-01-22T19:57:40Z")

</div>

Hello, This is my first time playing with neo4j, I modeled a graph that looks like this:

A ---\> B  
A ---\> C  
B ----\> D  
B ----\> E  
C ----\> F

now, i my input can contain several nodes, let's say i get "A and B", and i want to find the direct connections of A + the direct ones of B, meaning, to return the nodes:  
"B", "C", "D","E"

how to do it in a single query?

---

<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:** [January 23, 2022, 1:42am UTC](https://community.neo4j.com/t/get-directly-connected-nodes-for-multiple-nodes-in-one-query/50878/2 "2022-01-23T01:42:02Z")

</div>

Try the following:

unwind ['A', 'B'] as input  
match(n where n.name = input)  
match (n)-[:REL]-\>(m)  
return n.name, collect(m) as children

Replace the literal array ['A','B'] with your parameter containing your inputs. Also, replace the match condition with your unique identifier and the relationship type in the pattern match with yours. The result is a row for each of your inputs, with a list of each input's direct connected nodes.

Test Data:

 ![Screen Shot 2022-01-22 at 8.34.22 PM](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/0/3/035849990d515ff32e4002cba56101dec98794ba.png)

Result:

![Screen Shot 2022-01-22 at 8.34.39 PM](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/2/e/2ea31bfeb9795ad660e0aba9dba2d9f4b6df3a42.png)

If you prefer a list instead of a collection, replace the 'return' clause with this return clause 'return n.name, m', giving the following alternative result:

![Screen Shot 2022-01-22 at 8.40.10 PM](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/4/1/4169df17d02008a9826939c06eb6da17e779487c.png)

If you want just a list of the child nodes, replace the return clause with this return clause 'return collect(m)', giving the following result:

 ![Screen Shot 2022-01-22 at 8.44.40 PM](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/4/8/486d50352c27c9964189264b362bcec8e49dc518.png)
