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.

If node not exists, create multiple nodes and relations

Hello,

I want to MERGE a node:

MERGE (a: Article {URL: event.URL})

If the node does not exist, I need to do this:

ON CREATE FOREACH( site_name in CASE WHEN event.site_name is not null then [1] ELSE [] END |
    MERGE (w: Website { value: event.site_name})
    MERGE (w)-[:PUBLISHED]->(a))

    // all of the tag creation
    FOREACH( tag in CASE WHEN event.tags is not NULL then event.tags else [] END |
    Merge (t: Article_Tag {value: tag})
    CREATE (a)-[: HAS_ARICLE_TAG {date:event_datetime}]->(t))

I believe that ON CREATE only works with SET, but as above, i need to execute multiple statements.

I have tried ON CREATE FOREACH(ignoreme in case when event.article is not null then [1] else [] end |...\\ multiple statements) but this does not escape the SET problem.

Is there a way to create multiple nodes and relationships with an ON CREATE clause?

1 ACCEPTED SOLUTION

Keep in mind that nothing will happen if the MATCH fails to find anything, as there will be no rows at all for the FOREACH to execute upon. If you need that, then use OPTIONAL MATCH instead, which will result in a being null if no match is found.

View solution in original post

3 REPLIES 3

solved with


MATCH (a: Article {URL: event.URL})
FOREACH(ignoreme in case when a is null then [1] else [] end | .....statements...)

Keep in mind that nothing will happen if the MATCH fails to find anything, as there will be no rows at all for the FOREACH to execute upon. If you need that, then use OPTIONAL MATCH instead, which will result in a being null if no match is found.

ah actually this is important, and there is a typo in my solution, I was looking for exactly what you are suggesting, I will correct now.
Thank you Andrew!