Is it possible to determine if Cypher MERGE is creating or matching?

Consider a Neo4J 2.0 Cypher request

MERGE (u:User {id_str:"123"}) ON CREATE SET u.name="Bob", ON MATCH SET u.phone_num="555-4152" RETURN u 

This is a stupid question - don't worry about intentions here. The question is, how can I understand if this query was created or just found a node?

UPDATE

Perhaps I should further motivate why I want this behavior. Here's why: If node already exists, then you don’t need to go to the remote server (Twitter API in my case) and download all user metadata. It would be nice if ON CREATE somehow contacted the callback to tear down this data. It seems unlikely that this behavior is possible in Cypher. So, probably, I will need to make a match, and if get returns NULL, I will call the Twitter API, get the metadata and create it.

+1
source share
1 answer

I think contrary to the intention of MERGE . You would use it to describe how you want the chart to look after fulfilling your request, and not to find out what it looks like before the read and write operations should be strictly separated from each other in Cypher. However, you can set a property of a dummy type, for example, @LameCoder:

 MERGE (u:User {id_str:"123"}) ON CREATE SET u.onCreate = true WITH u, has(u.onCreate) as onCreate REMOVE n.onCreate RETURN u, u.onCreate 

will return (u), true if it is created, (u), false if not, and do not leave side effects. Or you can save timestamps, perhaps one for creating a node, and another for the last change to node - I think the example is from the documentation. it

 MERGE (u:User {id_str:"123"}) ON CREATE SET u.created = timestamp() ON MATCH SET u.lastModified = timestamp() RETURN u, has(u.lastModified) as onMerge 

will be similar to the request above.

+3
source

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


All Articles