Neo4j merge returns something only if it was created

Neo4j merge will create a new node if it does not exist. And it has ON CREATE and ON MATCH as two differences. However, is there a way to return other information if a node was created, if a node was mapped?

 MERGE (charlie { name:'Charlie Sheen' }) ON CREATE SET charlie.name = 'Charlie' RETURN charlie 

Something like: ON CREATE RETURN 1, ON MERGE RETURN 0

+5
source share
1 answer

Here's an example on the merge documentation page :

 MERGE (keanu:Person { name:'Keanu Reeves' }) ON CREATE SET keanu.created = timestamp() ON MATCH SET keanu.lastSeen = timestamp() RETURN keanu.name, has(keanu.lastSeen); 

This basically keeps your flag 0 or 1 in the existence or absence of the lastSeen attribute.

There is such an oddity in your request that you agree to “Charlie Sheen”, but then change the value that you agreed ( name ) to be “Charlie”. This is strange - it means that since you change it every time, even if you add an ON MATCH clause, it never works. You will create it every time and then change it, ensuring that it will be created new the next time you run this query. I would suggest that it is a bad idea to change the conditions of your match later in the request, unless this is a special circumstance.

+7
source

Source: https://habr.com/ru/post/1207113/


All Articles