Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create a variable and make its assignment based on certain conditions in a cypher query?

I am trying to create an array of values that will be assigned based on the outcome of a case test. This test will be inside a query that I already know works with a preset value in the query.

The query I am trying to embed in the case test is something like this:

WITH SPLIT (('07/28/2015'), '/' AS cd
MATCH (nodeA: NodeTypeA)-(r:ARelation)->(nodeB: NodeTypeB)
WITH cd, SPLIT (nodeA.ADate, '/') AS dd, nodeA, nodeB, r
WHERE
    (TOINT(cd[2])> TOINT(dd[2])) OR (TOINT(cd[2]= TOINT(dd[2]) AND ((TOINT(cd[0])> TOINT(dd[0])) OR (TOINT(cd[0])= TOINT(dd[0]) AND (TOINT(cd[1])>= TOINT(dd[1])))))
RETURN nodeA, nodeB, r

I want to replace the current date with whatever date will be 6 months from the current date, and I came up with something like this, though I am not sure where I would put it in my query or if it would even work (do I initialize the new variable for instance somehow?):

WHEN ((TOINT(cd[0])> 6))
THEN 
  TOINT(fd[2])=TOINT(cd[2])+1, TOINT(fd[0])=TOINT(cd[0])-6, TOINT(fd[1])=TOINT(cd[1])
ELSE
  TOINT(fd[2])=TOINT(cd[2]), TOINT(fd[0])=TOINT(cd[0])+6, TOINT(fd[1])=TOINT(cd[1])

fd would then replace the cd in the original query's WHERE segment. Where would my case test go, is it correctly written (and if not, what is wrong), and would I need something else added to make it all work?

like image 986
cluemein Avatar asked Jul 30 '15 20:07

cluemein


People also ask

What Cypher clauses can you use to create a node?

The CREATE clause is used to create nodes and relationships.

What are variables 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 .

How do you create a relationship between nodes in Neo4j?

We can create a relationship using the CREATE clause. We will specify relationship within the square braces “[ ]” depending on the direction of the relationship it is placed between hyphen “ - ” and arrow “ → ” as shown in the following syntax.

What is the correct syntax to create a node with a property in Neo4j?

In Neo4j, properties are the key-value pairs which are used by nodes to store data. CREATE statement is used to create node with properties, you just have to specify these properties separated by commas within the curly braces "{ }". Syntax: CREATE (node:label { key1: value, key2: value, . . . . . . . . . })


2 Answers

Just use a WITH block to do a computation and bind it to a new variable, like this:

WITH 2 + 2 as y RETURN y;

That basically assigns the value 4 to y.

In your query, you already have a big WITH block. Just put your computations in those, bound to new variables, and you can then refer to those variables in subsequent expressions.

Don't try to modify these variables, just create new ones (with new WITH blocks) as needed. If you need variables that can actually change, then...well hey you're working with a database, the ultimate way to store and update information. Create a new node, and then update it as you see fit. :)

like image 57
FrobberOfBits Avatar answered Oct 11 '22 10:10

FrobberOfBits


This is my proposed solution Explanation: I have declared four variables in my query i.e. name1, name2, ken and lana and I am using these variables for creating MATCH pattern (in the MATCH clause) and filtering those in the Where clause.

WITH    "Lau" AS name1,
        "L" AS name2,
        "Keanu Reeves" AS ken,
        "Lana Wachowski" AS lana
MATCH(x:Person{ name: ken})-[:ACTED_IN]->(m:Movie)<-[:ACTED_IN]-(y:Person),
        (x1:Person{name: lana})-[:DIRECTED]->(m)<-[:DIRECTED]-(y1:Person)

WHERE y.name CONTAINS name1 OR
      y.name CONTAINS name2 OR
      (y.name CONTAINS name1 AND y.name CONTAINS name2)
RETURN x, m, y, x1;
like image 28
Chandan Sharma Avatar answered Oct 11 '22 09:10

Chandan Sharma