boris
boris

Reputation: 11

Neo4j: when merging does not overwrite node properties

I want to modify a MERGE Cypher query so that the property field is not overwritten.

To update the node properties I use the following cypher query:

MERGE (n:Person {name:"me"}) ON MATCH SET n+={id:"2"} ON CREATE SET n={name:"me", id="1"} RETURN n

If I call the same query again with ON MATCH SET n+={id:"3"} the id will be updated.

How to make a query that adds the property if it is not existing but does not overwrite it if it is already present?

Upvotes: 1

Views: 709

Answers (1)

Christophe Willemsen
Christophe Willemsen

Reputation: 20185

First, you can just use plain properties, no need to do {map}..

Secondly, in case the MERGE creates the node, he will already have the property name:me on it :

MERGE (n:Person {name:'me'})
ON MATCH SET n.id = 2
ON CREATE SET n.id = 1
RETURN n

Upvotes: 2

Related Questions