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.

On match call

janezic
Graph Voyager

Does ON MATCH / ON CREATE work with SET only?

If yes: how would you solve the following problem?

MERGE (u2:User {user_email: "xyz@abc.at"})

ON MATCH
CALL apoc.do.when(
EXISTS((u2)-[:USER_TO_CUSTOMER]->(c)),'','MERGE (u2)-[:USER_TO_CUSTOMER {from: apoc.date.currentTimestamp()}]->(c)',{u2:u2, c:c})

THX, JJJ

1 ACCEPTED SOLUTION

Your idea was fully correct. It works perfectly with a chain of MERGEs. The only thing you have to do is to "outsource" the setting of the timestamp to an ON CREATE SET. If you don't do this and you repeat the query, it creates timestamped relationships one more time (because the pattern is not fully identical and the MERGE is not a MATCH but a CREATE.

View solution in original post

3 REPLIES 3

mdfrenchman
Graph Voyager

I'm not allowed to use APOC at work so I'm a cypher only guy. Here's, I think, what you're trying to do, in cypher.

// Some code to define c that I made up.
MATCH (c:Customer {keyProp: 'bob'})
// Create the user if it doesn't exist
MERGE (u2:User {user_email: 'xyz@abc.at'})
// If (u2)-[USER_TO_CUSTOMER]->(c) exists, then set the from timestamp
WITH u2, c
// if you want the rel to be created if it doesn't already exist, change this match to a merge
MATCH (u2)-[r:USER_TO_CUSTOMER]->(c)
SET r.from = datetime()

Thank you, but this is not exactly what I wanted to do.

I want to MERGE the (User)-node.

If the node is MATCHED (so it existed), there are 2 cases:

  • if a certain relationship already existed -> do nothing

  • else: create the relationship

If the node is CREATED (so it did not exist before the merge), I have to do a lot of actions (create other nodes, set properties, create relationships...)

So in contrast to your example, I have to have a distinction between ON MATCH and ON CREATE.

Your idea was fully correct. It works perfectly with a chain of MERGEs. The only thing you have to do is to "outsource" the setting of the timestamp to an ON CREATE SET. If you don't do this and you repeat the query, it creates timestamped relationships one more time (because the pattern is not fully identical and the MERGE is not a MATCH but a CREATE.