cancel
Showing results for 
Search instead for 
Did you mean: 

Multiple Inner UNWIND Loops

I have a data structure similar to the one below:

 

[
    {
        "ID":"1",
        "team":[
            "team1",
            "team2"
        ],
        "group":[
            "group1",
            "group2"
        ]
    },
    {
        "ID":"2",
        "team":[
        ],
        "group":[
            "group1"
        ]
    },
    {
        "ID":"3",
        "team":[
            "team1",
            "team3"
        ],
        "group":[
            "group3"
        ]
    }
]

 

Where I have a list of dictionaries, which have a list. I want to be able to run a query that iterates over the list like so:

 

for i in main_list:
    for j in i.team:
        # Code Here
    for k in i.group:
        # Code Here

 

But, when I try to do an UNWIND, such as the one below:

 

UNWIND main_list as i
    MERGE (n:USER {id:i.id})
    UNWIND i.teams as team
        MERGE (t:TEAM {name:team})
        MERGE (n)-[:BELONGS_TO]->(t)
    WITH i,n
    UNWIND i.groups as group
        MERGE (g:GROUP {name:group})
        MERGE (n)-[:BELONGS_TO]->(g)
RETURN count(main_list)

 

I think what the cypher query is doing is, but I'm not exactly sure.  

 

for i in main_list:
    for j in i.team:
        # Code Here
        for k in i.group:
            # Code Here

 

What is the best approach to handle multiple inner loops?

Thanks!

1 ACCEPTED SOLUTION

glilienfield
Ninja
Ninja

Use ‘forEach’ for each inner list instead of ‘unwind’. 

View solution in original post

2 REPLIES 2

glilienfield
Ninja
Ninja

Use ‘forEach’ for each inner list instead of ‘unwind’. 

That worked, thanks!