Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
03-02-2019 08:30 PM
I need to do a match then set a value if condition satisfies and then return the node.
I have tried the following:
MATCH (n:node {uid: "myid"})
WHERE (NONE(x IN n.arr WHERE x = "newentry") SET n.arr = n.arr + "newentry")
RETURN n;
The problem with this statement is that it will not return the node if "newentry" already exists in n.arr. But at the same time, I only want to add the new entry if it does not exist in n.arr. Irrespective of whether new entry was added or not, I have to return the node if there is a match.
Please help me with this.
Solved! Go to Solution.
03-03-2019 03:30 AM
you can just change your expression to a case
MATCH (n:node {uid: $id})
SET n.arr = case when $value IN n.arr then n.arr else n.arr + $value end
RETURN n
you can also use FOREACH for more complex nested conditionals
MATCH (n:node {uid: $id})
FOREACH (_ IN case when $value in n.arr then [] else [true] end |
SET n.arr = n.arr + $value
)
RETURN n;
03-03-2019 03:30 AM
you can just change your expression to a case
MATCH (n:node {uid: $id})
SET n.arr = case when $value IN n.arr then n.arr else n.arr + $value end
RETURN n
you can also use FOREACH for more complex nested conditionals
MATCH (n:node {uid: $id})
FOREACH (_ IN case when $value in n.arr then [] else [true] end |
SET n.arr = n.arr + $value
)
RETURN n;
03-03-2019 05:09 AM
Thank you @michael.hunger. I did not know we can use the 'case when' in the 'set' clause.
03-03-2019 05:20 AM
case ... end
is an expression so you can use it anywhere you use another expressions too.
All the sessions of the conference are now available online