# Proper use of UNWIND

**URL:** <https://community.neo4j.com/t/proper-use-of-unwind/15035>\
**Category:** Cypher\
**Created:** [February 24, 2020, 8:34pm UTC](https://community.neo4j.com/t/proper-use-of-unwind/15035 "2020-02-24T20:34:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lavanya\_kannan](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/lavanya_kannan/32/8545_2.png) [@lavanya\_kannan](https://community.neo4j.com/u/lavanya_kannan)\
**Post date:** [February 24, 2020, 8:34pm UTC](https://community.neo4j.com/t/proper-use-of-unwind/15035/1 "2020-02-24T20:34:26Z")

</div>

Hi, I am trying to add a new nodes for each WCC component and connect each alias node to it is component node. I am doing this with only one MATCH command and the UNWIND command - is this possible?

I tried

```auto
MATCH (n:alias) 
with n.GraphProperty_wcc_throughTopic as component, COLLECT(n) as component_nodes
WITH component, component_nodes, size(component_nodes) as component_size
MERGE (comp :WCC_component {GraphProperty_wcc : component, GraphProperty_wcc_size : component_size})
with (UNWIND $component_nodes) as component_node, component
MERGE (comp) <-[:in_WCC]- (component_node)

```

but I keep getting some error related to UNWIND that I do not understand:

```auto
Neo.ClientError.Statement.SyntaxError: Invalid input 'a': expected whitespace, comment or a relationship pattern (line 5, column 30 (offset: 302))
"with UNWIND $component_nodes as component_node, component, size_component"

```

Thanks,  
Lavanya  
^

---

<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:** [February 24, 2020, 10:06pm UTC](https://community.neo4j.com/t/proper-use-of-unwind/15035/2 "2020-02-24T22:06:08Z")

</div>

Try replacing  
with (UNWIND $component\_nodes) as component\_node, component

with  
UNWIND component\_nodes as component\_node  
MERGE (comp) \<-[:in\_WCC]- (component\_node

---

<div class="post-metadata">

**Author:** ![lavanya\_kannan](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/lavanya_kannan/32/8545_2.png) [@lavanya\_kannan](https://community.neo4j.com/u/lavanya_kannan)\
**Post date:** [February 25, 2020, 1:03am UTC](https://community.neo4j.com/t/proper-use-of-unwind/15035/3 "2020-02-25T01:03:21Z")

</div>

@ameyasoft

My query

```auto
MATCH (n:alias) 
with n.GraphProperty_wcc_throughTopic as component, COLLECT(n) as component_nodes
WITH component, component_nodes, size(component_nodes) as component_size
MERGE (comp :WCC_component {GraphProperty_wcc : component, GraphProperty_wcc_size : component_size})
with
UNWIND component_nodes as component_node
WITH component, component_node
MATCH (c :WCC_component {GraphProperty_wcc : component}) 
CREATE (c) <-[:in_WCC]- (component_node)

```

also returned the same error

```auto
Neo.ClientError.Statement.SyntaxError: Invalid input 'm': expected 'n/N' (line 6, column 10 (offset: 287))
"UNWIND component_nodes as component_node"

```

On the other hand, the following query WITHOUT the UNWIND command gave me the desired effect:

```auto
MATCH (n:alias) 
with n.GraphProperty_wcc_throughTopic as component, COLLECT(n) as component_nodes
WITH component, component_nodes, size(component_nodes) as component_size
CREATE (comp :WCC_component {GraphProperty_wcc : component, GraphProperty_wcc_size : component_size});

MATCH (n:alias) 
with n.GraphProperty_wcc_throughTopic as component, n AS component_node
WITH component, component_node
MATCH (c :WCC_component {GraphProperty_wcc : component}) 
CREATE (c) <-[:in_WCC]- (component_node)

```

I am still wondering why the UNWIND command is throwing this error.

---

<div class="post-metadata">

**Author:** ![andrew\_bowman](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/andrew_bowman/32/73_2.png) [@andrew\_bowman](https://community.neo4j.com/u/andrew_bowman)\
**Post date:** [February 25, 2020, 1:40am UTC](https://community.neo4j.com/t/proper-use-of-unwind/15035/4 "2020-02-25T01:40:22Z")

</div>

Hi again, you still have the UNWIND within the WITH clause:

```auto
with
UNWIND component_nodes as component_node

```

That said you WILL need a WITH clause between the MERGE and the UNWIND, but it can't be left blank like this, you need to include the variables that you want to keep in scope:

```auto
...
WITH component, component_nodes
UNWIND component_nodes as component_node
WITH component, component_node
MATCH (c :WCC_component {GraphProperty_wcc : component}) 
CREATE (c) <-[:in_WCC]- (component_node)

```

---

<div class="post-metadata">

**Author:** ![lavanya\_kannan](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/lavanya_kannan/32/8545_2.png) [@lavanya\_kannan](https://community.neo4j.com/u/lavanya_kannan)\
**Post date:** [February 25, 2020, 3:18pm UTC](https://community.neo4j.com/t/proper-use-of-unwind/15035/5 "2020-02-25T15:18:19Z")

</div>

Thanks a lot @andrew_bowman
