Birjolaxew
Birjolaxew

Reputation: 817

Chain UNWIND in cypher query

I am attempting to use two lists to create nodes/relationships in cypher. Using the solution for this related question, my current solution is:

MERGE (t:Test)
WITH t
UNWIND ["a","b"] AS name                      // first list
MERGE (t)-[:FOO_REL]->(a:Foo { name: name })
WITH DISTINCT t
UNWIND [100,200] AS id                        // second list
MERGE (t)-[:BAR_REL]->(b:Bar { id: id })

This works as long as both lists have entries in them. However, if the first list is empty then the second list is never unwound (and no :Bar nodes are created).

How would I go about chaining UNWINDs such that I can create nodes/relationships from two separate lists in a single query?

Upvotes: 0

Views: 1484

Answers (1)

Birjolaxew
Birjolaxew

Reputation: 817

I ended up using FOREACH instead of UNWIND. Since this uses parentheses around the inner query, they are clearly delimited from eachother:

MERGE (t:Test)
FOREACH (name in ["a","b"] | 
  MERGE (t)-[:FOO_REL]->(a:Foo { name: name })
)
FOREACH (id in [100,200] |
  MERGE (t)-[:BAR_REL]->(b:Bar { id: id })
)

Upvotes: 2

Related Questions