Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4J create temp variable within Cypher

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

like image 456
ErocCatlun Avatar asked Feb 06 '15 21:02

ErocCatlun


People also ask

How do I declare a variable in Neo4j?

When you reference parts of a pattern or a query, you do so by naming them. The names you give the different parts are called variables. The variables are n and b .

WHAT IS WITH clause in Cypher?

The WITH clause allows query parts to be chained together, piping the results from one to be used as starting points or criteria in the next. It is important to note that WITH affects variables in scope. Any variables not included in the WITH clause are not carried over to the rest of the query.

What is unwind in Neo4j?

With UNWIND , you can transform any list back into individual rows.

What is a clause in Neo4j?

Administration clauses These comprise clauses used to manage databases, schema and security; further details can found in Database management and Access control. Clause. Description. CREATE | DROP | START | STOP DATABASE. Create, drop, start or stop a database.


1 Answers

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;
like image 194
FrobberOfBits Avatar answered Sep 23 '22 11:09

FrobberOfBits