I have a given CSV file:
1,2016-10-09T21:14:44Z,Anna Long,"[{"_id": "1", "objectType": "primaryTag"}]"
2,2016-10-10T14:03:51Z,Jonathan Collier,"[{"_id": "2", "objectType": "primaryTag"}, {"_id": "3", "objectType": "secondaryTag"}]"
3,2016-10-09T21:42:35Z,Michelle Wheeler,"[{"_id": "4", "objectType": "primaryTag"}, {"_id": "5", "objectType": "secondaryTag"}, {"_id": "6", "objectType": "secondaryTag"}]"
Now I try to create Nodes and Relationships from the JSON array within the CSV file. I can import the CSV file with this command:
LOAD CSV FROM file:///<my_file> AS row
Also, I am able to get the JSON array with this query:
WITH apoc.convert.fromJsonList(row[3]) AS tags
Now I want to create a relationship between each tag in a JSON array for each one of the rows in the CSV file respectively.
So as an example, let us take the third line from the CSV file. We have 3 objects in the JSON array, therefore I would like to create 3 relationships. A relationship between the tags with the id 4&5, 4&6, and 5&6.
Sadly I am completely stuck here and not sure if I have to work with UNWIND or FOREACH.
EDIT
I solved the problem as followed (all relevant nodes are already imported in the database):
LOAD CSV FROM file:///<my_file> AS row
MATCH (q:Question {id: row[0]})--(t1:Tag)
WITH COLLECT(t1) AS tags, row[0] AS question_id
FOREACH (i IN range(0, size(tags) - 2) |
FOREACH (node1 IN [tags[i]] |
FOREACH (j IN range(i+1, size(tags) - 1) |
FOREACH (node2 IN [tags[j]] |
MERGE (node1)-[c:CONNECTED_TO]-(node2)
)
)
)
)
The code works but it has 4 FOREACH loops. I still believe that there is a more beautiful approach. So feel free to answer my question.