- 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 - 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.
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
Step 3 - Click on Execute button to create Customer node with 3 properties.
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
Step 3 - Click on Execute button to create CreditCard node with 4 properties.
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
Step 3 - Click on Execute button to run this command.
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
Step 3 - Click on Execute button to run this command.
If you observe the Data Browser message, it shows that CreditCard node is created with 4 properties in the Neo4j Database.