Deeswoc
Deeswoc

Reputation: 180

Nested UNWIND not returning from higher UNWIND in Cypher query

I have the following query

UNWIND $towns as town
MERGE (a:area:Town {Name: town.name, uuid: town.uuid}) with a, town
MATCH (p:parish{Name: town.parish}) with a, p, town
MERGE (a)-[:in]-(p) with a, town
UNWIND town.categories as category
MATCH (c:LocationCategory {uuid: category}) 
MERGE (a)-[:category]-(c) 
RETURN a

town.categories can be null so I'm guessing the second UNWIND doesn't return a row for the null values of the categories, fine. But I'don't care about returning any row from the second unwind because it might not return any. But I want to return all the original a nodes. How do I break the second unwind so I can return a regularly as if it wasn't there?

Upvotes: 0

Views: 721

Answers (1)

Christophe Willemsen
Christophe Willemsen

Reputation: 20185

You can change your UNWIND for a FOREACH by using a MERGE instead of a MATCH in the second batch.

That would then solve the issue for the second unwind breaking your results in case of nulls

UNWIND $towns as town
MERGE (a:area:Town {Name: town.name, uuid: town.uuid}) with a, town
MATCH (p:parish{Name: town.parish}) with a, p, town
MERGE (a)-[:in]-(p) with a, town
FOREACH (category IN town.categories |
  MERGE (c:LocationCategory {uuid: category}) 
  MERGE (a)-[:category]-(c)
)
RETURN a

Upvotes: 1

Related Questions