cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Neo4j, empty list in first unwind stop second unwind from executing

I have two unwinds, that create some relationships and nodes, but if the list for the first unwinds is empty, the second unwind doesn't execute.

How can I fix that?

CALL apoc.periodic.iterate(
    "
        UNWIND $POSTS as post
        RETURN post
    ",
    "
        MERGE (p:Post{id: post.id})

        WITH p, post
        UNWIND post.tags as value
        MERGE (t:Tag{tag: value})
        MERGE (t)-[:has_tag]->(p)

        WITH p, post
        UNWIND post.user_mentions as user_mention
        MERGE (u1:User{id: user_mention})
        MERGE (p)-[:mentions]->(u1)
    ",
    {batchSize: 500, params: {POSTS: $POSTS}, iterateList:true}
)

Example results

Parameters, with non-empty tags

[
    {
        "id": 123,
        "tags": [1],
        "user_mentions": [123, 234],
    }
]

Graph created in database - Expected result

 

SmKtb

Parameters, with empty tags

[
    {
        "id": 123,
        "tags": [],
        "user_mentions": [123, 234],
    }
]

Graph created in the database (Lacking 'mentions' relationships) - Unexpected result

lzo1F

1 REPLY 1

glilienfield
Ninja
Ninja

The behavior is expected. If the 'unwind' on line 10 results in zero records, the query stops. Try the following:

 

 

CALL apoc.periodic.iterate(
    "
        UNWIND $POSTS as post
        RETURN post
    ",
    "
        MERGE (p:Post{id: post.id})

        forEach(value in post.tags |
            MERGE (t:Tag{tag: value})
            MERGE (t)-[:has_tag]->(p)
        )

        forEach(user_mention in post.user_mentions |
          MERGE (u1:User{id: user_mention})
          MERGE (p)-[:mentions]->(u1)
        )
    ",
    {batchSize: 500, params: {POSTS: $POSTS}, iterateList:true}
)

 

 

Test 1: No missing data

Screen Shot 2023-01-26 at 2.39.47 PM.png 

Screen Shot 2023-01-26 at 2.40.27 PM.png

Test 2: Missing Tag data

Screen Shot 2023-01-26 at 2.40.45 PM.png

Screen Shot 2023-01-26 at 2.41.16 PM.png

Test 3: Missing user_mentions data

Screen Shot 2023-01-26 at 2.42.07 PM.png

Screen Shot 2023-01-26 at 2.42.33 PM.png