Avoiding duplicate nodes when existence is unknown

I'm using Neo4j Desktop to develop the cypher queries I will need for my use case.

Suppose that my graph may or may not be seeded with data based on the following query.

CREATE (parent:Entity {id: 'Dan'})
CREATE (child:Component {id: 'Bob', value: 'aaa'})
CREATE (child)-[:CHILD_OF]->(parent)
RETURN parent, child

and then run this query.

MERGE (parent:Entity {id: 'Dan'})
MERGE (child:Component {id: 'Bob', value: 'bbb'})
MERGE (child)-[:CHILD_OF]->(parent)
RETURN parent, child

When this query is run I don't know whether the parent entity 'Dan' exists or not, nor do I know whether the child or relationship exists.

What I need is a query which creates the parent, child, and relationship if they don't exist. If they do exist, then all it should do is update the value of the child.

But this query ends up creating a new child so that the result is that after running this query, the parent has two children, one with the old value and one with the new value.

I don't think I can use MATCH because that presumes the nodes exist.

The behavior is because your match for the child ‘Bob’ also includes a value property that is different from the existing ‘Bob’ node created with the Create clause.

To remedy this, you need to merge only on the I’d, so it finds the existing node regardless of the value of value. You can then use the ‘on match’ clause with the merge to update the value property to ‘bbb’.

Try this

MERGE (parent:Entity {id: 'Dan'})
MERGE (child:Component {id: 'Bob'})
ON MATCH
SET child.value = 'bbb'
MERGE (child)-[:CHILD_OF]->(parent)
RETURN parent, child

Ah, light bulbs coming on. That works. Thank you Gary.

1 Like