How to create a node only in case if it not exists in neo4j?

Viewed 5286

I need a query which creates the entity. I need something like IF NOT EXISTS in SQL. I checked to CREATE UNIQUE command but it's deprecated now.

1 Answers

You can use MERGE statement to only create a node if it does not exist yet in the graph. Check the documenation

MERGE (p:Person{name:"Marina"})

This will search for a node labelled Person with a name property "Marina" and if it does not find one it will create a new node with that label and name property.

If you want to create some other nodes if your original nodes does not exist then you need OPTIONAL MATCH.

OPTIONAL MATCH (p:Person{name:"Marina"})
// filter when the "Marina" node doesn't exist yet
WITH p WHERE p IS NULL
...
Related