- Neo4j Tutorial
- Neo4j - Home
- Neo4j - Overview
- Neo4j - Data Model
- Neo4j - Environment Setup
- Neo4j - Building Blocks
- Neo4j CQL Write Clauses
- Neo4j - Merge Command
- Neo4j - Set Clause
- Neo4j - Delete Clause
- Neo4j - Remove Clause
- Neo4j - Foreach Clause
- Neo4j CQL Read Clause
- Neo4j - Match Clause
- Neo4j - Optional Match Clause
- Neo4j - Where Clause
- Neo4j - Count Function
- Neo4j CQL General Clauses
- Neo4j - Return Clause
- Neo4j - Order By Clause
- Neo4j - Limit Clause
- Neo4j - Skip Clause
- Neo4j - With Clause
- Neo4j - Unwind Clause
- Neo4j CQL Functions
- Neo4j - String Functions
- Neo4j - Aggregation Function
- Neo4j CQL Admin
- Neo4j - Backup & Restore
- Neo4j - Index
- Neo4j - Create Unique Constraint
- Neo4j - Drop Unique
- Neo4j Useful Resources
- Neo4j - Quick Guide
- Neo4j - Useful Resources
- Neo4j - Discussion
Neo4j CQL - MERGE
Neo4j CQL MERGE command is used -
To create nodes, relationships and properties
To retrieve data from database
MERGE command is a combination of CREATE Command and MATCH command.
MERGE = CREATE + MATCH
Neo4j CQL MERGE command searches for given pattern in the graph, if it exists then it returns the results
If it does NOT exist in the graph, then it creates new node/relationship and returns the results.
Neo4j CQL MERGE syntax
MERGE (<node-name>:<label-name> { <Property1-name>:<Pro<rty1-Value> ..... <Propertyn-name>:<Propertyn-Value> })
Syntax Description
S.No. | Syntax Element | Description |
---|---|---|
1. | MERGE | It is a Neo4j CQL keyword. |
2. | <node-name> | It is the name of a Node or a Relationship. |
3. | <label-name> | It is the label name of a Node or a Relationship. |
4. | <property_name> | It is the property name of a Node or a Relationship. |
5. | <property_value> | It is the property value of a Node or a Relationship. |
6. | : | Use colon(:) operator to separate property name and value of a Node or a Relationship. |
NOTE -
Neo4j CQL MERGE command syntax is similar to CQL CREATE Command.
We are going to perform the following operations by using both commands -
Create a Profile node with one property: Id, Name
Create same Profile node with same properties: Id, Name
Retrieve all Profile node details and observe the results
We are going to perform these operations by using CREATE command
Neo4j CQL CREATE Example
This example performs all above operations by using CREATE,MATCH and RETURN commands to create a Google+ Profile.
Operation (1): Create a Profile node with properties: Id, Name
Step 1 - Open Neo4j Data Browser
Step 2 - Type the below command on Data Browser
CREATE (gp1:GoogleProfile1 {Id: 201401, Name:"Apple"})
Step 3 - Click on "Execute" button and observe the results.
If we observe the success message, it has added two properties to GoogleProfile1 node.
Operation (2): Create same Profile node with same properties: Id, Name.
Step 1 - Open Neo4j Data Browser
Step 2 - Type the below command on Data Browser
CREATE (gp1:GoogleProfile1 {Id: 201401, Name:"Apple"})
Step 3 - Click on "Execute" button and observe the results.
If we observe the success message, it has added two properties to GoogleProfile1 node.
Operation (3): Retrieve all Profile node details and observe the results.
Step 1 - Open Neo4j Data Browser
Step 2 - Type the below command on Data Browser
MATCH (gp1:GoogleProfile1) RETURN gp1.Id,gp1.Name
Step 3 - Click on "Execute" button and observe the results.
If we observe the above query results, it shows 2 rows with duplicate values.
CQL CREATE command does check whether this node available or not, it just creates new node in the Database.
By observing these results, we can say that CREATE command always adds new Node to the Database.
Neo4j CQL MERGE Example
This example performs same set of above operations by using MERGE and RETURN commands to create a Google+ Profile.
Operation (1): Create a Profile node with properties: Id, Name
Step 1 - Open Neo4j Data Browser
Step 2 - Type the below command on Data Browser
MERGE (gp2:GoogleProfile2{ Id: 201402,Name:"Nokia"})
Step 3 - Click on "Execute" button and observe the results.
If we observe the success message, it has added two properties to GoogleProfile1 node.
Operation (2): Create same Profile node with same properties: Id, Name.
Step 1 - Open Neo4j Data Browser
Step 2 - Type the below command on Data Browser
MERGE (gp2:GoogleProfile2{ Id: 201402,Name:"Nokia"})
Step 3 - Click on "Execute" button and observe the results.
If we observe the success message, it has added two properties to GoogleProfile1 node.
Operation (3): Retrieve all Profile node details and observe the results
Step 1 - Open Neo4j Data Browser
Step 2 - Type the below command on Data Browser
MATCH (gp1:GoogleProfile1) RETURN gp1.Id,gp1.Name
Step 3 - Click on "Execute" button and observe the results.
If we observe the above query results, it shows only one row because CQL MERGE command checks whether this node is available in the database or not. If it does NOT exist, it creates new node. Otherwise, it does not create new one.
By observing these results, we can say that CQL MERGE command adds new Node to the Database only if it does not exist.