Neo4j CQL - CREATE + MATCH + RETURN



In Neo4j CQL, We cannot use MATCH or RETURN commands alone, so we should combine those two commands to retrieve data from Database.

Example

This Example demonstrates, how to create two Nodes with Properties and Relationship between those two nodes .

NOTE - We are going to create two nodes : Customer and CreditCard with relationships.

  • Customer node contains :id, name, dob properties

  • CreditCard node contains: id, number, cvv, expiredate properties

  • Customer to CreditCard Relationship: DO_SHOPPING_WITH

  • CreditCard to Customer Relationship: ASSOCIATED_WITH

We will work on this example in the following steps -

  • Create Customer Node
  • Create CreditCard Node
  • Observe the previously created two nodes: Customer and CreditCard
  • Create Relationship between Customer & CreditCard Nodes
  • View the newly created Relationship details
  • View the each node and relationship properties in detail

NOTE - We will discuss first three steps in this chapter. We will discuss rest of the steps in coming chapters

Create Customer Node

Step 1 - Open Neo4j Data Browser.

Neo4j CQL Tutorial

Step 2 - Type the below command at dollar prompt in Data Browser.

CREATE (e:Customer{id:"1001",name:"Abc",dob:"01/10/1982"})

Here -

  • e is a node name
  • Here Customer is a node label name
  • id,name and dob are a property names of Customer node
Neo4j CQL Tutorial

Step 3 - Click on Execute button to create Customer node with 3 properties.

Neo4j CQL Tutorial

If you observe the Data Browser message, it shows that a node is created with 3 properties in the Neo4j Database.

Create CreditCard Node

Step 1 - Open Neo4j Data Browser.

Step 2 - Type the below command at dollar prompt in Data Browser.

CREATE (cc:CreditCard{id:"5001",number:"1234567890",cvv:"888",expiredate:"20/17"})

Here c is a node name

Here CreditCard is a node label name

id,number,cvv and expiredate are a property names of CreditCard node

Neo4j CQL Tutorial

Step 3 - Click on Execute button to create CreditCard node with 4 properties.

Neo4j CQL Tutorial

If you observe the Data Browser message, it shows that a node is created with 4 properties in the Neo4j Database.

Observe Nodes

Now we have created two nodes: Customer and CreditCard

We need to view these two nodes details by using Neo4j CQL MATCH command with RETURN clause

View Customer Node Details

Step 1 - Open Neo4j Data Browser

Step 2 - Type the below command at dollar prompt in Data Browser.

MATCH (e:Customer)
RETURN e.id,e.name,e.dob

Here e is a node name

Here Customer is a node label name

id,name and dob are a property names of Customer node

Neo4j CQL Tutorial

Step 3 - Click on Execute button to run this command.

Neo4j CQL Tutorial

If you observe the Data Browser message, it shows that Customer node is created with 3 properties in the Neo4j Database.

View CreditCard Node Details

Step 1 - Open Neo4j Data Browser

Step 2 - Type the below command at dollar prompt in Data Browser.

MATCH (cc:CreditCard)
RETURN cc.id,cc.number,cc.cvv,cc.expiredate

Here cc is a node name

Here CreditCard is a node label name

id,number,cvv,expiredate are a property names of CreditCard node

Neo4j CQL Tutorial

Step 3 - Click on Execute button to run this command.

Neo4j CQL Tutorial

If you observe the Data Browser message, it shows that CreditCard node is created with 4 properties in the Neo4j Database.

Advertisements