Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j Add/update properties if node exists

Tags:

neo4j

cypher

I want to be able to update/enlarge my Neo4j database by uploading a newer version of that database OR part of that database.

Of what I've found I can use MERGE to add new nodes just if they don't already exists. But in that process, how do I in a slimmed way add new properties to that existing node if they don't exist?

I e, if I have a node 'John' of 'Age:34' and 'Hair:brown' and upload 'John'/'Age:34'/'Coat:Yellow' - how do I get 'John'/'Age:34'/'Hair:brown'/'Coat:Yellow'.

like image 799
user1806627 Avatar asked Feb 07 '16 15:02

user1806627


1 Answers

You could merge the node on John (or the primary identifying attribute). And then set the properties after the successful merge.

You could set them all at once with a map for all attributes

MERGE (n:Node {name: 'John'})
SET n = {name: 'John', age: 34, coat: 'Yellow', hair: 'Brown'}
RETURN n

If you just wanted to replace the attributes age and coat, you could do this instead.

MERGE (n:Node {name: 'John'})
SET n.age = 34, n.coat = 'Yellow'
RETURN n 

Or you could add it as a map too

MERGE (n:Node {name: 'John'})
SET n += {age: 34, coat: 'Yellow'}
RETURN n 
like image 69
Dave Bennett Avatar answered Oct 14 '22 02:10

Dave Bennett