Sharmila
Sharmila

Reputation: 75

Difference between merge and create in Cypher

  1. Could anyone please tell me the difference between Merge and Create in Cypher Querying.
  2. How Neo4j stores data physically?

Thanks in advance..

Upvotes: 3

Views: 1391

Answers (1)

FrobberOfBits
FrobberOfBits

Reputation: 18002

CREATE does just what it says. It creates, and if that means creating duplicates, well then it creates. MERGE does the same thing as create, but also checks to see if a node already exists with the properties you specify. If it does, then it doesn't create. This helps avoid duplicates. Here's an example: I use CREATE twice to create a person with the same name.

neo4j-sh (?)$ create (p:Person {name: "Bob"});
+-------------------+
| No data returned. |
+-------------------+
Nodes created: 1
Properties set: 1
Labels added: 1
9 ms
neo4j-sh (?)$ create (p:Person {name: "Bob"});
+-------------------+
| No data returned. |
+-------------------+
Nodes created: 1
Properties set: 1
Labels added: 1
5 ms

So now when we query, there are two Bob's.

neo4j-sh (?)$ match (p:Person {name:"Bob"}) return p;
+--------------------------+
| p                        |
+--------------------------+
| Node[222124]{name:"Bob"} |
| Node[222125]{name:"Bob"} |
+--------------------------+
2 rows
46 ms

Let's MERGE in another Bob and see what happens.

neo4j-sh (?)$ merge (p:Person {name:"Bob"});
+--------------------------------------------+
| No data returned, and nothing was changed. |
+--------------------------------------------+
2 ms
neo4j-sh (?)$ match (p:Person {name:"Bob"}) return p;
+--------------------------+
| p                        |
+--------------------------+
| Node[222124]{name:"Bob"} |
| Node[222125]{name:"Bob"} |
+--------------------------+
2 rows
11 ms

Bob already existed, so MERGE did nothing here. Querying again, the same two Bobs are present. Had there been no Bobs in the database, MERGE would have done the same thing as CREATE.

Upvotes: 7

Related Questions