Neo4j - Quick Guide
Neo4j - Quick Guide
Neo4j - Overview
Neo4j is the world's leading open source Graph Database which is developed using
Java technology. It is highly scalable and schema free (NoSQL).
Graph database is a database used to model the data in the form of graph. In here,
the nodes of a graph depict the entities while the relationships depict the association
of these nodes.
Neo4j is a popular Graph Database. Other Graph Databases are Oracle NoSQL
Database, OrientDB, HypherGraphDB, GraphBase, InfiniteGraph, and AllegroGraph.
Nowadays, most of the data exists in the form of the relationship between different
objects and more often, the relationship between the data is more valuable than the
data itself.
Relational databases store highly structured data which have several records storing
the same type of data so they can be used to store structured data and, they do not
store the relationships between the data.
Unlike other databases, graph databases store relationships and connections as first-
class entities.
The data model for graph databases is simpler compared to other databases and,
they can be used with OLTP systems. They provide features like transactional
integrity and operational availability.
RDBMS Vs Graph Database
Following is the table which compares Relational databases and Graph databases.
Advantages of Neo4j
Following are the advantages of Neo4j.
Flexible data model − Neo4j provides a flexible simple and yet powerful
data model, which can be easily changed according to the applications and
industries.
Real-time insights − Neo4j provides results based on real-time data.
Connected and semi structures data − Using Neo4j, you can easily
represent connected and semi-structured data.
Easy retrieval − Using Neo4j, you can not only represent but also easily
retrieve (traverse/navigate) connected data faster when compared to other
databases.
Features of Neo4j
Following are the notable features of Neo4j −
Data model (flexible schema) − Neo4j follows a data model named native
property graph model. Here, the graph contains nodes (entities) and these
nodes are connected with each other (depicted by relationships). Nodes and
relationships store data in key-value pairs known as properties.
In Neo4j, there is no need to follow a fixed schema. You can add or remove
properties as per requirement. It also provides schema constraints.
Neo4j Graph Database stores all of its data in Nodes and Relationships. We neither
need any additional RRBMS Database nor any SQL database to store Neo4j database
data. It stores its data in terms of Graphs in its native format.
Neo4j uses Native GPE (Graph Processing Engine) to work with its Native graph
storage format.
Nodes
Relationships
Properties
Here, we have represented Nodes using Circles. Relationships are represented using
Arrows. Relationships are directional. We can represent Node's data in terms of
Properties (key-value pairs). In this example, we have represented each Node's Id
property within the Node's Circle.
Step 1 − Visit the Neo4j official site using https://neo4j.com/. On clicking, this link
will take you to the homepage of neo4j website.
Step 2 − As highlighted in the above screenshot, this page has a Download button
on the top right hand side. Click it.
Step 3 − This will redirect you to the downloads page, where you can download the
community edition and the enterprise edition of Neo4j. Download the community
edition of the software by clicking the respective button.
Step 4 − This will take you to the page where you can download community version
of Neo4j software compatible with different operating systems. Download the file
respective to the desired operating system.
Step 6 − Accept the license agreement and proceed with the installation. After
completion of the process, you can observe that Neo4j is installed in your system.
Once the server starts, you can observe that the database directory is populated as
shown in the following screenshot.
Working with Neo4j
As discussed in the previous chapters, neo4j provides an in-built browse application
to work with Neo4j. You can access Neo4j using the URL http://localhost:7474/
Nodes
Properties
Relationships
Labels
Data Browser
Node
Node is a fundamental unit of a Graph. It contains properties with key-value pairs as
shown in the following image.
Properties
Property is a key-value pair to describe Graph Nodes and Relationships.
Key = Value
Where Key is a String and Value may be represented using any Neo4j Data types.
Relationships
Relationships are another major building block of a Graph Database. It connects two
nodes as depicted in the following figure.
Here, Emp and Dept are two different nodes. "WORKS_FOR" is a relationship
between Emp and Dept nodes.
As it denotes, the arrow mark from Emp to Dept, this relationship describes −
Each relationship contains one start node and one end node.
As this relationship arrow mark represents a relationship from "Emp" node to "Dept"
node, this relationship is known as an "Incoming Relationship" to "Dept" Node and
"Outgoing Relationship" to "Emp" node.
Id = 123
Labels
Label associates a common name to a set of nodes or relationships. A node or
relationship can contain one or more labels. We can create new labels to existing
nodes or relationships. We can remove the existing labels from the existing nodes or
relationships.
From the previous diagram, we can observe that there are two nodes.
Left side node has a Label: "Emp" and the right side node has a Label: "Dept".
http://localhost:7474/browser/
Neo4j Data Browser is used to execute CQL commands and view the output.
Type commands after the dollar symbol and click the "Execute" button to run your
commands.
It interacts with Neo4j Database Server, retrieves and displays the results just below
the dollar prompt.
Use "VI View" button to view the results in diagrams format. The above diagram
shows results in "UI View" format.
Use "Grid View" button to view the results in Grid View. The following diagram shows
the same results in "Grid View" format.
When we use "Grid View" to view our Query results, we can export them into a file in
two different formats.
CSV
Click the "Export CSV" button to export the results in csv file format.
JSON
Click the "Export JSON" button to export the results in JSON file format.
However, if we use "UI View" to see our Query results, we can export them into a file
in only one format: JSON
Neo4j CQL
We will discuss all Neo4j CQL commands, clauses and functions syntax, usage and
examples in-detail in the subsequent chapters.
CQL Operators
Following are the list of operators supported by Neo4j Cypher Query language.
Syntax
Following is the syntax for creating a node using Cypher Query Language.
CREATE (node_name);
Example
CREATE (sample)
Step 1 − Open the Neo4j desktop App and start the Neo4j Server as shown in the
following screenshot.
Step 2 − Open your browser, copy paste the following URL in your address bar
http://localhost:7474/. This will give you the built-in browser app of Neo4j with a
dollar prompt as shown in the following screenshot.
Step 3 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
To verify the creation of the node type, execute the following query in the dollar
prompt.
This query returns all the nodes in the database (we will discuss this query in detail
in the coming chapters).
On executing, this query shows the created node as shown in the following
screenshot.
Creating Multiple Nodes
The create clause of Neo4j CQL is also used to create multiple nodes at the same
time. To do so, you need to pass the names of the nodes to be created, separated by
a comma.
Syntax
Following is the syntax to create multiple nodes using the CREATE clause.
CREATE (node1),(node2)
Example
CREATE (sample1),(sample2)
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
To verify the creation of the node, type and execute the following query in the dollar
prompt.
This query returns all the nodes in the database (we will discuss this query in detail
in the coming chapters).
On executing, this query shows the created node as shown in the following
screenshot.
Creating a Node with a Label
A label in Neo4j is used to group (classify) the nodes using labels. You can create a
label for a node in Neo4j using the CREATE clause.
Syntax
Following is the syntax for creating a node with a label using Cypher Query
Language.
CREATE (node:label)
Example
CREATE (Dhawan:player)
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
Verification
To verify the creation of the node, type and execute the following query in the dollar
prompt.
This query returns all the nodes in the database (we will discuss this query in detail
in the coming chapters).
On executing, this query shows the created node as shown in the following
screenshot.
Syntax
Example
Following is a sample Cypher Query which creates a node with multiple labels in
Neo4j.
CREATE (Dhawan:person:player)
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
On executing, you will get the following result.
Verification
To verify the creation of the node, type and execute the following query in the dollar
prompt.
This query returns all the nodes in the database (we will discuss this query in detail
in the coming chapters).
On executing, this query shows the created node as shown in the following
screenshot.
Create Node with Properties
Properties are the key-value pairs using which a node stores data. You can create a
node with properties using the CREATE clause. You need to specify these properties
separated by commas within the flower braces “{ }”.
Syntax
Example
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
To verify the creation of the node, type and execute the following query in the dollar
prompt.
This query returns all the nodes in the database (we will discuss this query in detail
in the coming chapters).
On executing, this query shows the created node as shown in the following
screenshot.
Returning the Created Node
Throughout the chapter, we used the MATCH (n) RETURN n query to view the
created nodes. This query returns all the existing nodes in the database.
Instead of this, we can use the RETURN clause with CREATE to view the newly
created node.
Syntax
Example
Following is a sample Cypher Query which creates a node with properties and returns
it.
CREATE (Dhawan:player{name: "Shikar Dhawan", YOB: 1985, POB: "Delhi"}) RETURN
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
Create relationships
Create a relationship between the existing nodes
Creating Relationships
We can create a relationship using the CREATE clause. We will specify relationship
within the square braces “[ ]” depending on the direction of the relationship it is
placed between hyphen “ - ” and arrow “ → ” as shown in the following syntax.
Syntax
CREATE (node1)-[:RelationshipType]->(node2)
Example
First of all, create two nodes Ind and Dhawan in the database, as shown below.
CREATE (Dhawan)-[r:BATSMAN_OF]->(Ind)
Result
Syntax
Example
Following is a sample Cypher Query which creates a relationship using the match
clause.
MATCH (a:player), (b:Country) WHERE a.name = "Shikar Dhawan" AND b.name = "In
CREATE (a)-[r: BATSMAN_OF]->(b)
RETURN a,b
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
Syntax
Following is the syntax to create a relationship with label and properties using the
CREATE clause.
Example
Following is a sample Cypher Query which creates a relationship with label and
properties.
MATCH (a:player), (b:Country) WHERE a.name = "Shikar Dhawan" AND b.name = "In
CREATE (a)-[r:BATSMAN_OF {Matches:5, Avg:90.75}]->(b)
RETURN a,b
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
Syntax
Following is the syntax to create a path in Neo4j using the CREATE clause.
Example
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
Neo4j CQL MERGE command searches for a given pattern in the graph. If it exists,
then it returns the results.
If it does NOT exist in the graph, then it creates a new node/relationship and returns
the results.
Syntax
Before proceeding to the examples in this section, create two nodes in the database
with labels Dhawan and Ind. Create a relationship of type “BATSMAN_OF” from
Dhawan to Ind as shown below.
Syntax
Example 1
Following is a sample Cypher Query which merges a node into Neo4j (based on
label). When you execute this query, Neo4j verifies whether there is any node with
the label player. If not, it creates a node named “Jadeja” and returns it.
If, there exists any node with the given label, Neo4j returns them all.
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
On executing, you will get the following result. Since you have already created a
node named “Dhawan” with the label “player” in the database, Neo4j returns it as
shown in the following screenshot.
Example 2
Now, try to merge a node named “CT2013” with a label named Tournament. Since
there are no nodes with this label, Neo4j creates a node with the given name and
returns it.
Step 1 − Open the Neo4j desktop App and Start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
On executing, you will get the following result. As discussed, since there is no node
with the given label (Tournament). Neo4j creates and returns the specified node as
shown in the following screenshot.
Merging a Node with Properties
You can also merge a node with a set of properties. If you do so, Neo4j searches for
an equal match for the specified node, including the properties. If it doesn’t find any,
it creates one.
Syntax
Example
Following is a sample Cypher Query to merge a node using properties. This query
tries to merge the node named “jadeja” using properties and label. Since there is no
such node with the exact label and properties, Neo4j creates one.
Step 1 − Open the Neo4j desktop App and Start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
On executing, you will get the following result. As discussed, since there are no
nodes with the specified label and properties, it creates one, as shown in the
following screenshot.
OnCreate and OnMatch
Whenever, we execute a merge query, a node is either matched or created. Using on
create and on match, you can set properties for indicating whether the node is
created or matched.
Syntax
Example
Following is a sample Cypher Query which demonstrates the usage of OnCreate and
OnMatch clauses in Neo4j. If the specified node already exists in the database, then
the node will be matched and the property with key-value pair isFound = "true" will
be created in the node.
If the specified node doesn’t exist in the database, then the node will be created,
and within it a property with a key-value pair isCreated ="true" will be created.
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
On executing, you will get the following result. As discussed, since there is no node
with the specified details, Neo4j created it along with the property isFound as
shown in the following screenshot.
Merge a Relationship
Just like nodes, you can also merge the relationships using the MERGE clause.
Example
Following is a sample Cypher Query which merges a relationship using the MATCH
clause in Neo4j. This query tries to merge a relationship named WINNERS_OF
between the nodes “ind” (label: Country & name: India) and ICC13 (label:
Tournament & name: ICC Champions Trophy 2013).
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
On executing, you will get the following result. Since the specified relation doesn’t
exist in the database, Neo4j creates one as shown in the following screenshot.
In the same way, you can merge multiple relationships and undirected relationships
too.
Set a property
Remove a property
Setting a Property
Using the SET clause, you can create a new property in a node.
Syntax
MATCH (node:label{properties . . . . . . . . . . . . . . })
SET node.property = value
RETURN node
Example
Before proceeding with the example, first create a node named Dhawan as shown
below.
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screnshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
On executing, you will get the following result. Here you can observe that a property
with a key-value pair highestscore/187 is created in the node named “Dhawan”.
Removing a Property
You can remove an existing property by passing NULL as value to it.
Syntax
Following is the syntax of removing a property from a node using the SET clause.
Example
Before proceeding with the example, first create a node “jadeja” as shown below.
Following is a sample Cypher Query which removes the property named POB from
this node using the SET clause as shown below.
MATCH (Jadeja:player {name: "Ravindra Jadeja", YOB: 1988, POB: "NavagamGhed"}
SET Jadeja.POB = NULL
RETURN Jadeja
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
On executing, you will get the following result. Here you can observe that the
variable named POB was deleted.
Syntax
Following is the syntax to create multiple properties in a node using the SET clause.
Example
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
On executing, you will get the following result. Here you can observe that properties
named POB and HS were created.
Syntax
MATCH (n {properties . . . . . . . })
SET n :label
RETURN n
Example
Before proceeding with the example, first create a node “Anderson” as shown below.
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
On executing, you will get the following result. Here you can observe that the label
named “player” is added to the node.
Following is the syntax to set multiple labels to an existing node using the SET
clause.
MATCH (n {properties . . . . . . . })
SET n :label1:label2
RETURN n
Example
Before proceeding with the example, first create a node named “Ishant” as shown
below.
Following is a sample Cypher Query used to create multiple labels on a node using
the SET clause.
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
On executing, you will get the following result. Here you can observe that two labels
- person and player – are added to the node named Ishant.
Neo4j - Delete Clause
You can delete nodes and relationships from a database using the DELETE clause.
Query
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
This will delete all the nodes and relationships from your neo4j database and make it
empty.
Syntax
Following is the syntax to delete a particular node from Neo4j using the DELETE
clause.
Before proceeding with the example, create a node “Ishant” in the Neo4j database
as shown below.
Following is a sample Cypher Query which deletes the above created node using the
DELETE clause.
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
On executing, you will get the following result. Here you can observe that the
specified node is deleted.
The main difference between Neo4j CQL DELETE and REMOVE commands is −
Removing a Property
You can remove a property of a node using MATCH along with the REMOVE clause.
Syntax
Following is the syntax to remove a property of a node using the REMOVE clause.
MATCH (node:label{properties . . . . . . . })
REMOVE node.property
RETURN node
Example
Before proceeding with the example, create a node named Dhoni as shown below.
Following is a sample Cypher Query to remove the above created node using the
REMOVE clause.
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
On executing, you will get the following result. Here, you can observe that the node
named POB was deleted.
Removing a Label From a Node
Similar to property, you can also remove a label from an existing node using the
remove clause.
Syntax
Example
Following is a sample Cypher Query to remove a label from an existing node using
the remove clause.
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
On executing, you will get the following result. Here, you can observe that the label
was deleted from the node.
Removing Multiple Labels
You can also remove multiple labels from an existing node.
Syntax
Example
Before proceeding with the example, create a node Ishant as shown below.
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
On executing, you will get the following result. Here, you can observe that the
specified labels were deleted from the node.
Syntax
Following is the syntax of the FOREACH clause.
Example
Before proceeding with the example, create a path p in Neo4j database as shown
below.
CREATE p = (Dhawan {name:"Shikar Dhawan"})-[:TOPSCORRER_OF]->(Ind{name:
"India"})-[:WINNER_OF]->(CT2013{name: "Champions Trophy 2013"})
RETURN p
Following is a sample Cypher Query which adds a property to all the nodes along the
path using the FOREACH clause.
MATCH p = (Dhawan)-[*]->(CT2013)
WHERE Dhawan.name = "Shikar Dhawan" AND CT2013.name = "Champions Trophy 20
FOREACH (n IN nodes(p)| SET n.marked = TRUE)
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
On executing, you will get the following result.
Verification
To verify the creation of the node, type and execute the following query in the dollar
prompt.
This query returns all the nodes in the database (we will discuss this query in detail
in the coming chapters).
On executing, this query shows the created node as shown in the following
screenshot.
Example
Before proceeding with the example, create 3 nodes and 2 relationships as shown
below.
Following is the query which returns all the nodes in Neo4j database.
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
Syntax
Following is the syntax to get all the nodes under a specific label.
MATCH (node:label)
RETURN node
Example
Following is a sample Cypher Query, which returns all the nodes in the database
under the label player.
MATCH (n:player)
RETURN n
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
Match by Relationship
You can retrieve nodes based on relationship using the MATCH clause.
Syntax
Following is the syntax of retrieving nodes based on the relationship using the
MATCH clause.
Following is a sample Cypher Query to retrieve nodes based on relationship using the
MATCH clause.
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
Query
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
OPTIONAL MATCH is similar to the match clause, the only difference being it returns
null as a result of the missing parts of the pattern.
Syntax
Following is the syntax of the OPTIONAL MATCH with relationship.
Example
Following is a sample Cypher Query which tries to retrieve the relations from the
node ICCT2013. Since there are no such nodes, it returns null.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
On executing, you will get the following result. Here you can observe that since there
are no matches for the required pattern, Neo4j returned null.
Neo4j - Where Clause
Like SQL, Neo4j CQL has provided WHERE clause in CQL MATCH command to filter
the results of a MATCH Query.
Syntax
MATCH (label)
WHERE label.country = "property"
RETURN label
Example
Before proceeding with the example, create five nodes in the database as shown
below.
MATCH (player)
WHERE player.country = "India"
RETURN player
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
On executing, you will get the following result.
Syntax
Following is the syntax to use WHERE clause in Neo4j with multiple conditions.
MATCH (emp:Employee)
WHERE emp.name = 'Abc' AND emp.name = 'Xyz'
RETURN emp
Example
Following is a sample Cypher Query which filters the nodes in the Neo4j database
using two conditions.
MATCH (player)
WHERE player.country = "India" AND player.runs >=175
RETURN player
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
On executing, you will get the following result.
Example
MATCH (n)
WHERE (n)-[: TOP_SCORER_OF]->( {name: "India", result: "Winners"})
RETURN n
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
On executing, you will get the following result. Here you can observe that Neo4j
returned the node, which has the relation TOP_SCORER_OF to the country with the
node having the name India.
Neo4j - Count Function
Assume we have created a graph in the database with the following details.
Count
The count() function is used to count the number of rows.
Syntax
Example
Following is a sample Cypher Query which demonstrates the usage of the count()
function.
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
Group Count
The COUNT clause is also used to count the groups of relationship types.
Example
Following is a sample Cypher Query which counts and returns the number of nodes
participating in each relation.
Match(n{name: "India", result: "Winners"})-[r]-(x)
RETURN type (r), count(*)
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Return properties
Return all elements
Returning Nodes
You can return a node using the RETURN clause.
Syntax
Example
Before proceeding with the example, create 3 nodes and 2 relationships as shown
below.
Following is a sample Cypher Query which creates a node named Dhoni and returns
it.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
Syntax
Following is the syntax to return multiple nodes using the return clause.
Example
Following is a sample Cypher Query to return multiple nodes using the return clause.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
On executing, you will get the following result. Here you can observe that Neo4j
returned 2 nodes.
Returning Relationships
You can also return relationships using the Return clause.
Syntax
CREATE (node1)-[Relationship:Relationship_type]->(node2)
RETURN Relationship
Example
Following is a sample Cypher Query which creates two relationships and returns
them.
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
Syntax
Example
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
Example
Following is an example Cypher Query to return all the elements in the database.
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
Example
Following is a sample Cypher Query which returns the column POB as Place Of Birth.
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
Syntax
MATCH (n)
RETURN n.property1, n.property2 . . . . . . . .
ORDER BY n.property
Example
Before proceeding with the example, create 5 nodes in Neo4j database as shown
below.
Following is a sample Cypher Query which returns the above created nodes in the
order of the runs scored by the player using the ORDERBY clause.
MATCH (n)
RETURN n.name, n.runs
ORDER BY n.runs
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
Syntax
Following is the syntax to arrange nodes by multiple properties using the ORDERBY
clause.
MATCH (n)
RETURN n
ORDER BY n.age, n.name
Example
Following is a sample Cypher Query which arranges the nodes created earlier in this
chapter based on the properties - runs and country.
MATCH (n)
RETURN n.name, n.runs, n.country
ORDER BY n.runs, n.country
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
Syntax
MATCH (n)
RETURN n
ORDER BY n.name DESC
Example
MATCH (n)
RETURN n.name, n.runs
ORDER BY n.runs DESC
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
Syntax
MATCH (n)
RETURN n
ORDER BY n.name
LIMIT 3
Example
Before proceeding with the example, create 5 nodes in the Neo4j database as shown
below.
Following is a sample Cypher Query which returns the nodes created above in a
descending order and limits the records in the result to 3.
MATCH (n)
RETURN n.name, n.runs
ORDER BY n.runs DESC
LIMIT 3
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
Example
Following is a sample Cypher Query which limits the records using an expression.
MATCH (n)
RETURN n.name, n.runs
ORDER BY n.runs DESC
LIMIT toInt(3 * rand())+ 1
To execute the above query, carry out the following steps −
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
Example
Following is a sample Cypher Query which returns all the nodes in the database
skipping the first 3 nodes.
MATCH (n)
RETURN n.name, n.runs
ORDER BY n.runs DESC
SKIP 3
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
Example
Following is a sample Cypher Query which uses the SKIP clause with an expression.
MATCH (n)
RETURN n.name, n.runs
ORDER BY n.runs DESC
SKIP toInt (2*rand())+ 1
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
Syntax
Following is the syntax of the WITH clause.
MATCH (n)
WITH n
ORDER BY n.property
RETURN collect(n.property)
Example
Following is a sample Cypher Query which demonstrates the usage of the WITH
clause.
MATCH (n)
WITH n
ORDER BY n.name DESC LIMIT 3
RETURN collect(n.name)
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
On executing, you will get the following result.
Neo4j - Unwind Clause
The unwind clause is used to unwind a list into a sequence of rows.
Example
Following is a sample Cypher Query which unwinds a list.
UNWIND [a, b, c, d] AS x
RETURN x
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
On executing, you will get the following result.
Neo4j - String Functions
Like SQL, Neo4J CQL has provided a set of String functions to use them in CQL
Queries to get the required results.
Here, we are going to discuss some of the important and frequently used functions.
In this section, we are going to discuss about two important DBA tasks.
Note − These steps are applicable to Windows Operating System only. We should
use similar kind of commands to do the same steps in other operating systems.
C:\Ne04j2.0db
Before taking a database backup, the first and foremost thing we should do is
shutdown the Neo4j Database server.
Now we can observe that our database folder contains working backup files
Neo4j - Index
Neo4j SQL supports Indexes on node or relationship properties to improve the
performance of the application. We can create indexes on properties for all nodes,
which have the same label name.
Create an Index
Delete an Index
Creating an Index
Neo4j CQL provides "CREATE INDEX" command to create indexes on Node or
Relationship properties.
Syntax
Example
Before proceeding with the example, create a node Dhawan as shown below.
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown below.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
Deleting an Index
Neo4j CQL provides a "DROP INDEX" command to drop an existing index of a Node
or Relationshis property.
Syntax
Example
Following is a sample Cypher Query to create an index on the node named “Dhawan”
in Neo4j.
DROP INDEX ON:player(Dhawan)
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
Like SQL, Neo4j database also supports UNIQUE constraint on node or relationship
properties. UNIQUE constraint is used to avoid duplicate records and to enforce data
integrity rule.
Syntax
Example
Before proceeding with the example, create 4 nodes as shown below.
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in
browser app of Neo4j using the URL http://localhost:7474/ as shown in the
following screenshot.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
Verification
Now, try to add another node with a redundant id value. Here, we are trying to
create a node with id 002.
CREATE (Jadeja:player {id:002, name: "Ravindra Jadeja", YOB: 1988, POB: "Nava
If you execute this query, you will get an error message as shown in the following
screenshot.
Neo4j - Drop Unique
We have already discussed creating UNIQUE constraint operations with examples in
the previous chapter. In this chapter, we will discuss dropping UNIQUE constraint
operation with examples.
Syntax
Following is the syntax for dropping a UNIQUE constraint in Neo4j.
Example
Following is a sample Cypher Query to remove the UNIQUE constraint on the
property id.
Step 2 − Copy and paste the desired query in the dollar prompt and press the play
button (to execute the query) highlighted in the following screenshot.
Result
On executing, you will get the following result.