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 ElementDescription
1.MERGEIt 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

Neo4j CQL Tutorial

Step 2 - Type the below command on Data Browser

CREATE (gp1:GoogleProfile1 {Id: 201401, Name:"Apple"})
Neo4j CQL Tutorial

Step 3 - Click on "Execute" button and observe the results.

Neo4j CQL Tutorial

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"})
Neo4j CQL Tutorial

Step 3 - Click on "Execute" button and observe the results.

Neo4j CQL Tutorial

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
Neo4j CQL Tutorial

Step 3 - Click on "Execute" button and observe the results.

Neo4j CQL Tutorial

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"})
Neo4j CQL Tutorial

Step 3 - Click on "Execute" button and observe the results.

Neo4j CQL Tutorial

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"})
Neo4j CQL Tutorial

Step 3 - Click on "Execute" button and observe the results.

Neo4j CQL Tutorial

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
Neo4j CQL Tutorial

Step 3 - Click on "Execute" button and observe the results.

Neo4j CQL Tutorial

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.

Advertisements