Proper use of UNWIND

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

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:

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
^

Try replacing
with (UNWIND $component_nodes) as component_node, component

with
UNWIND component_nodes as component_node
MERGE (comp) <-[:in_WCC]- (component_node

@ameyasoft

My query

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

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:

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.

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

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:

...
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)
1 Like

Thanks a lot @andrew_bowman