How do I choose to create a node based on whether it exists in the database?

I have the following code
<"MERGE (a:Person{id:}) MERGE (b:Place{}) MERGE (c:event{}) MERGE (a)–[:was_at]->(b) MERGE (a)–[:is]->(c) MERGE (c)–[:took_place_at]->(b)">

I wish to create the node a only if the id does not already exist in the database. How can I do so? Do i make use of apoc?

Hello @james.irvin and welcome to the Neo4j community :slight_smile:

MERGE is the right keyword to create if it doesn't exist, what is the issue with your query?


I tried the following but it threw this error. I wish to only create a new person node if there isnt a person node in the existing database that has the exact same properties.

org.neo4j.driver.exceptions.ClientException: Invalid input 'R': expected

MERGE (n:Person{id:}) 
MERGE (m:Place{}) 
MERGE (o:Thing{})
WITH n,m,o
OPTIONAL MATCH (n) – [:present_at] -> x with n,m,o, collect (distinct x) as known_place
OPTIONAL MATCH (m) – [:is] -> y with n,m,o, collect (distinct y) as known_thing
FOREACH (a in ( CASE WHEN NOT m IN known_place THEN [1] ELSE [] END ) CREATE (n)-[:present_at] ->(m))
FOREACH (a in ( CASE WHEN NOT o IN known_thing THEN [1] ELSE [] END ) CREATE (m)-[:is] ->(o))