Skip to Content

MERGE vs CREATE in Cypher

CREATE always writes a new node or relationship, even if an identical one exists. MERGE first looks for the whole pattern and creates it only if it finds nothing, so running it twice leaves one copy. Use CREATE for things that are new by definition (an event, a log entry, a new post), and MERGE for things you want exactly once (a user by email, a tag by name, one FOLLOWS edge between two users).

CREATE (:Tag {name: 'neo4j'}) // run twice: two Tag nodes MERGE (:Tag {name: 'neo4j'}) // run twice: one Tag node

Side by side

CREATEMERGE
Pattern already existscreates another copyuses the existing one
Pattern missingcreates itcreates all of it
Reads before writingnoyes: it looks the pattern up first
Set properties conditionallynoON CREATE SET / ON MATCH SET
null in the patternallowed (the property is just not stored)error: “Cannot merge … because of null property value”
Duplicate on a unique keyfails with ConstraintValidationFailedmatches the existing node instead

Three MERGE pitfalls

Extra properties in the pattern. Everything inside the braces is part of the match. If Alice exists with no name, this does not match her and tries to create a second Alice:

// Don't: name is part of the lookup MERGE (u:User {email: 'alice@acme.io', name: 'Alice'}) // Do: MERGE on the key, SET the rest MERGE (u:User {email: 'alice@acme.io'}) SET u.name = 'Alice'

With a uniqueness constraint on email the first form fails with Neo.ClientError.Schema.ConstraintValidationFailed; without one it creates a duplicate.

Merging a path with new nodes in it. MERGE (u:User {email: $e})-[:AUTHORED]->(p:Post {title: $t}) creates the user again when only the post is missing. MERGE nodes separately, then the relationship. See Don’t MERGE a whole path.

Concurrency without a constraint. MERGE is “match, else create”. Ten concurrent MERGEs of the same new key created between 5 and 10 nodes per run in our test (15 runs, Neo4j 5.26 Community) when the key had no constraint, and always exactly one when it had one. Pair every MERGE key with a uniqueness constraint.

Relationships: CREATE adds, MERGE deduplicates

MATCH (a:User {email: 'alice@acme.io'}), (b:User {email: 'bob@acme.io'}) CREATE (a)-[:FOLLOWS]->(b) // run twice: two FOLLOWS relationships MATCH (a:User {email: 'alice@acme.io'}), (b:User {email: 'bob@acme.io'}) MERGE (a)-[:FOLLOWS]->(b) // run twice: one FOLLOWS relationship

Neo4j allows several relationships of the same type between the same two nodes, so CREATE never fails here. If you write MERGE (a)-[:FOLLOWS]-(b) without an arrow, it matches a relationship in either direction and only creates one when there is none (in our test, pointing from a to b).

In Cyphra

The typed client maps to these clauses as follows:

CyphraCypher it runs
db.user.create, createManyCREATE
db.user.upsertMERGE on the unique key, with ON CREATE SET and ON MATCH SET
connect in create / updateMATCH the target, then MERGE the relationship
nested create of a related nodeCREATE the node and its relationship

So, like CREATE, a duplicate db.user.create on a @unique field fails (with a UniqueConstraintError), while upsert matches the existing node (see Upsert with MERGE for its concurrency behavior). And like MERGE, connect is idempotent: calling it twice for the same pair leaves one FOLLOWS edge.

import { db } from "./db.js"; await db.user.create({ data: { email: "bob@acme.io" } }); for (let i = 0; i < 2; i++) { await db.user.update({ where: { email: "alice@acme.io" }, data: { following: { connect: { email: "bob@acme.io" } } }, }); } const [{ edges }] = await db.$cypher<{ edges: number }>` MATCH (:User {email: ${"alice@acme.io"}})-[r:FOLLOWS]->(:User {email: ${"bob@acme.io"}}) RETURN count(r) AS edges `; // edges === 1

When you need two parallel relationships of the same type between one pair (one per event, say), connect will not create them: use CREATE through db.$cypher, or model the event as a node.

Last updated on