5

So my Top-Level problem is I am trying to return whether a MERGE resulted in the creation of a new Node or not.

In order to do this I was thinking I could just create a simple temp boolean setting it to TRUE using ON CREATE

How I imagine it working:

MERGE(: Person {id:'Tom Jones'})
WITH false as temp_bool
ON CREATE set temp_bool = true
RETURN temp_bool

Obviously this does not work.

I am looking for a way to create arbitrary temp values within a Cypher query, and have the ability to return those variables in the end.

Thanks

ErocCatlun
  • 83
  • 1
  • 7

2 Answers2

14

You can do what you want, here's how (combination of my first answer, with @cybersam's addition). You just do it with a node property you create and then remove, instead of an unbound variable as you've been trying.

MERGE(tom:Person {id:'Tom Jones'})
ON CREATE set tom.temp_bool = true
ON MATCH set tom.temp_bool = false
WITH tom, tom.temp_bool AS result
REMOVE tom.temp_bool
RETURN result;
cybersam
  • 63,203
  • 6
  • 53
  • 76
FrobberOfBits
  • 17,634
  • 4
  • 52
  • 86
  • 2
    Actually, you can get rid of the temp property in the same query: `MERGE(tom:Person {id:'Tom Jones'}) ON CREATE set tom.temp_bool = true ON MATCH set tom.temp_bool = false WITH tom, tom.temp_bool AS result REMOVE tom.temp_bool RETURN result;` – cybersam Feb 06 '15 at 22:23
2

In simple merging cases like this where maximum one node could be created, a cleaner way to achieve what you are looking for could be checking the result stats. I case of using Bolt API you should check:

results.consume().counters.nodes_created = 1
gonzaedu61
  • 91
  • 5