cancel
Showing results for 
Search instead for 
Did you mean: 

Iterate through two lists?

andreperez
Graph Buddy

I'm trying to associate two lists, in a one-to-one relationship.
This query is returning the following: all objects in p are merged with only the first element of r.
I need something like id1(n)-[:HAS_ID]-id2(n)

MATCH p=(n:ID1)

MATCH r=(o:ID2)

WITH p, r LIMIT 100

UNWIND nodes(p) AS ids1

UNWIND nodes(r) AS ids2

MERGE (ids1)-[:HAS_ID]->(ids2)
1 ACCEPTED SOLUTION

Cobra
Ninja
Ninja
MATCH (a:ID1)
WITH a LIMIT 100
WITH collect(a) AS ids1
MATCH (b:ID2)
WITH ids1, b LIMIT 100
WITH ids1, collect(b) AS ids2
UNWIND range(0, size(ids1)-1) AS i
WITH ids1[i] AS n, ids2[i] AS m
MERGE (n)-[:HAS_ID]->(m)

View solution in original post

4 REPLIES 4

Cobra
Ninja
Ninja

Hello @andreperez

MATCH (a:ID1)
WITH a LIMIT 100
WITH collect(a) AS ids1
MATCH (b:ID2)
WITH ids1, b LIMIT 100
WITH ids1, collect(b) AS ids2
UNWIND range(0, size(ids1)) AS i
MERGE (ids1[i])-[:HAS_ID]->(ids2[i])

Regards,
Cobra

Your query makes total sense to me, but Neo4J is complaining 😧

Invalid input 'ids1': expected "(", "allShortestPaths" or "shortestPath" (line 8, column 8 (offset: 175))
"MERGE (ids1[i])-[:HAS_ID]->(ids2[i])"
        ^

Cobra
Ninja
Ninja
MATCH (a:ID1)
WITH a LIMIT 100
WITH collect(a) AS ids1
MATCH (b:ID2)
WITH ids1, b LIMIT 100
WITH ids1, collect(b) AS ids2
UNWIND range(0, size(ids1)-1) AS i
WITH ids1[i] AS n, ids2[i] AS m
MERGE (n)-[:HAS_ID]->(m)

Thank you, works perfectly