Category: neo4j

  • Khóa học miễn phí Neo4j – Discussion nhận dự án làm có lương

    Discuss Neo4j



    Neo4j is one of the popular Graph Databases and Cypher Query Language (CQL). Neo4j is written in Java Language. This tutorial explains the basics of Neo4j, Java with Neo4j, and Spring DATA with Neo4j. The tutorial is divided into sections such as Neo4j Introduction, Neo4j CQL, Neo4j CQL Functions, Neo4j Admin, etc. Each of these sections contain related topics with simple and useful examples.


    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí Neo4j – Aggregation Function nhận dự án làm có lương

    Neo4j – Aggregation Function



    Like SQL, Neo4j CQL has provided some aggregation functions to use in RETURN clause. It is similar to GROUP BY clause in SQL.

    We can use this RETURN + Aggregation Functions in MATCH command to work on a group of nodes and return some aggregated value.

    AGGREGATION Functions List

    Following is the list of aggregation functions in Neo4j.

    Sr.No Function & Description
    1

    It returns the number of rows returned by MATCH command.

    2

    It returns the maximum value from a set of rows returned by MATCH command.

    3

    It returns the minimum value from a set of rows returned by MATCH command.

    4

    It returns the summation value of all rows returned by MATCH command.

    5

    It returns the average value of all rows returned by MATCH command.


    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí Neo4j – Remove Clause nhận dự án làm có lương

    Neo4j – Remove Clause



    The REMOVE clause is used to remove properties and labels from graph elements (Nodes or Relationships).

    The main difference between Neo4j CQL DELETE and REMOVE commands is −

    • DELETE operation is used to delete nodes and associated relationships.
    • REMOVE operation is used to remove labels and properties.

    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.

    CREATE (Dhoni:player {name: "MahendraSingh Dhoni", YOB: 1981, POB: "Ranchi"})
    

    Following is a sample Cypher Query to remove the above created node using the REMOVE clause.

    MATCH (Dhoni:player {name: "MahendraSingh Dhoni", YOB: 1981, POB: "Ranchi"})
    REMOVE Dhoni.POB
    RETURN Dhoni
    

    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.

    Browser App

    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.

    Remove Ranchi

    Result

    On executing, you will get the following result. Here, you can observe that the node named POB was deleted.

    Remove Property Result

    Removing a Label From a Node

    Similar to property, you can also remove a label from an existing node using the remove clause.

    Syntax

    Following is the syntax to remove a label from a node.

    MATCH (node:label {properties . . . . . . . . . . . })
    REMOVE node:label
    RETURN node
    

    Example

    Following is a sample Cypher Query to remove a label from an existing node using the remove clause.

    MATCH (Dhoni:player {name: "MahendraSingh Dhoni", YOB: 1981, POB: "Ranchi"})
    REMOVE Dhoni:player
    RETURN Dhoni
    

    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.

    Browser App

    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.

    Remove Player

    Result

    On executing, you will get the following result. Here, you can observe that the label was deleted from the node.

    Remove Result

    Removing Multiple Labels

    You can also remove multiple labels from an existing node.

    Syntax

    Following is the syntax to remove multiple labels from a node.

    MATCH (node:label1:label2 {properties . . . . . . . . })
    REMOVE node:label1:label2
    RETURN node
    

    Example

    Before proceeding with the example, create a node Ishant as shown below.

    CREATE (Ishant:player:person {name: "Ishant Sharma", YOB: 1988, POB: "Delhi"})
    

    Following is a sample Cypher Query to remove multiple labels from a node.

    MATCH (Ishant:player:person {name: "Ishant Sharma", YOB: 1988, POB: "Delhi"})
    REMOVE Ishant:player:person
    RETURN Ishant
    

    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.

    Browser App

    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.

    Remove Multi Label

    Result

    On executing, you will get the following result. Here, you can observe that the specified labels were deleted from the node.

    Multi Label Result

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí Neo4j – Delete Clause nhận dự án làm có lương

    Neo4j – Delete Clause



    You can delete nodes and relationships from a database using the DELETE clause.

    Deleting All Nodes and Relationships

    Following is the query to delete all the nodes and the relationships in the database using the DELETE clause.

    Query

    MATCH (n) DETACH DELETE n
    

    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.

    Browser App

    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.

    Detach Match

    This will delete all the nodes and relationships from your neo4j database and make it empty.

    Deleting a Particular Node

    To delete a particular node, you need to specify the details of the node in the place of “n” in the above query.

    Syntax

    Following is the syntax to delete a particular node from Neo4j using the DELETE clause.

    MATCH (node:label {properties . . . . . . . . . .  })
    DETACH DELETE node
    

    Example

    Before proceeding with the example, create a node “Ishant” in the Neo4j database as shown below.

    CREATE (Ishant:player {name: "Ishant Sharma", YOB: 1988, POB: "Delhi"})
    

    Following is a sample Cypher Query which deletes the above created node using the DELETE clause.

    MATCH (Ishant:player {name: "Ishant Sharma", YOB: 1988, POB: "Delhi"})
    DETACH DELETE Ishant
    

    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.

    Browser App

    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.

    Particular Node

    Result

    On executing, you will get the following result. Here you can observe that the specified node is deleted.

    Deleted Node

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí Neo4j – Optional Match Clause nhận dự án làm có lương

    Neo4j – Optional Match Clause



    The OPTIONAL MATCH clause is used to search for the pattern described in it, while using nulls for missing parts of the pattern.

    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.

    MATCH (node:label {properties. . . . . . . . . . . . . .})
    OPTIONAL MATCH (node)-->(x)
    RETURN x
    

    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.

    MATCH (a:Tornament {name: "ICC Champions Trophy 2013"})
    OPTIONAL MATCH (a)-->(x)
    RETURN x
    

    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.

    Browser App

    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.

    Optional Match

    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.

    Pattern Required

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí Neo4j – Match Clause nhận dự án làm có lương

    Neo4j – Match Clause



    In this chapter, we will learn about Match Clause and all the functions that can be performed using this clause.

    Get All Nodes Using Match

    Using the MATCH clause of Neo4j you can retrieve all nodes in the Neo4j database.

    Example

    Before proceeding with the example, create 3 nodes and 2 relationships as shown below.

    CREATE (Dhoni:player {name: "MahendraSingh Dhoni", YOB: 1981, POB: "Ranchi"})
    CREATE (Ind:Country {name: "India", result: "Winners"})
    CREATE (CT2013:Tornament {name: "ICC Champions Trophy 2013"})
    CREATE (Ind)-[r1:WINNERS_OF {NRR:0.938 ,pts:6}]->(CT2013)
    
    CREATE(Dhoni)-[r2:CAPTAIN_OF]->(Ind)
    CREATE (Dhawan:player{name: "shikar Dhawan", YOB: 1995, POB: "Delhi"})
    CREATE (Jadeja:player {name: "Ravindra Jadeja", YOB: 1988, POB: "NavagamGhed"})
    
    CREATE (Dhawan)-[:TOP_SCORER_OF {Runs:363}]->(Ind)
    CREATE (Jadeja)-[:HIGHEST_WICKET_TAKER_OF {Wickets:12}]->(Ind)
    

    Following is the query which returns all the nodes in Neo4j database.

    MATCH (n) RETURN n
    

    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.

    Browser App

    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.

    Match Return

    Result

    On executing, you will get the following result.

    Executing Result

    Getting All Nodes Under a Specific Label

    Using match clause, you can get all the nodes under a specific label.

    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
    

    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.

    Browser App

    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.

    N Player

    Result

    On executing, you will get the following result.

    Specific Label

    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.

    MATCH (node:label)<-[: Relationship]-(n)
    RETURN n
    

    Example

    Following is a sample Cypher Query to retrieve nodes based on relationship using the MATCH clause.

    MATCH (Ind:Country {name: "India", result: "Winners"})<-[: TOP_SCORER_OF]-(n)
    RETURN n.name
    

    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.

    Browser App

    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.

    Top Scorer

    Result

    On executing, you will get the following result.

    Match Relationship

    Delete All Nodes

    You can delete all the nodes using the MATCH clause.

    Query

    Following is the query to delete all the nodes in Neo4j.

    MATCH (n) detach delete n
    

    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.

    Browser App

    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.

    Desired Press

    Result

    On executing, you will get the following result.

    Deleted Relationship

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí Neo4j – Foreach Clause nhận dự án làm có lương

    Neo4j – Foreach Clause



    The FOREACH clause is used to update data within a list whether components of a path, or result of aggregation.

    Syntax

    Following is the syntax of the FOREACH clause.

    MATCH p = (start node)-[*]->(end node)
    WHERE start.node = "node_name" AND end.node = "node_name"
    FOREACH (n IN nodes(p)| SET n.marked = TRUE)
    

    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 2013"
    FOREACH (n IN nodes(p)| SET n.marked = TRUE)
    

    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.

    Browser App

    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.

    For each

    Result

    On executing, you will get the following result.

    Set Properties

    Verification

    To verify the creation of the node, type and execute the following query in the dollar prompt.

    MATCH (n) RETURN n
    

    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.

    Created Result

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí Neo4j – Return Clause nhận dự án làm có lương

    Neo4j – Return Clause



    The RETURN clause is used return nodes, relationships, and properties in Neo4j. In this chapter, we are going to learn how to −

    • Return nodes
    • Return multiple nodes
    • Return relationships
    • Return properties
    • Return all elements
    • Return a variable with column alias

    Returning Nodes

    You can return a node using the RETURN clause.

    Syntax

    Following is a syntax to return nodes using the RETURN clause.

    Create (node:label {properties})
    RETURN node
    

    Example

    Before proceeding with the example, create 3 nodes and 2 relationships as shown below.

    Create (Dhoni:player {name: "MahendraSingh Dhoni", YOB: 1981, POB: "Ranchi"})
    CREATE (Ind:Country {name: "India", result: "Winners"})
    CREATE (CT2013:Tornament {name: "ICC Champions Trophy 2013"})
    CREATE (Ind)-[r1:WINNERS_OF {NRR:0.938 ,pts:6}]->(CT2013)
    CREATE(Dhoni)-[r2:CAPTAIN_OF]->(Ind)
    

    Following is a sample Cypher Query which creates a node named Dhoni and returns it.

    Create (Dhoni:player {name: "MahendraSingh Dhoni", YOB: 1981, POB: "Ranchi"})
    RETURN Dhoni
    

    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.

    Browser App

    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

    Result

    On executing, you will get the following result.

    Create Player

    Returning Multiple Nodes

    You can also return multiple nodes using the return clause.

    Syntax

    Following is the syntax to return multiple nodes using the return clause.

    CREATE (Ind:Country {name: "India", result: "Winners"})
    CREATE (CT2013:Tornament {name: "ICC Champions Trophy 2013"})
    RETURN Ind, CT2013
    

    Example

    Following is a sample Cypher Query to return multiple nodes using the return clause.

    CREATE (Ind:Country {name: "India", result: "Winners"})
    CREATE (CT2013:Tornament {name: "ICC Champions Trophy 2013"})
    RETURN Ind, CT2013
    

    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.

    Browser App

    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.

    Multi Node

    Result

    On executing, you will get the following result. Here you can observe that Neo4j returned 2 nodes.

    Create Tornament

    Returning Relationships

    You can also return relationships using the Return clause.

    Syntax

    Following is the syntax to return relationships using the RETURN clause.

    CREATE (node1)-[Relationship:Relationship_type]->(node2)
    RETURN Relationship
    

    Example

    Following is a sample Cypher Query which creates two relationships and returns them.

    CREATE (Ind)-[r1:WINNERS_OF {NRR:0.938 ,pts:6}]->(CT2013)
    CREATE(Dhoni)-[r2:CAPTAIN_OF]->(Ind)
    RETURN r1, r2
    

    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.

    Browser App

    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.

    Relationship Return

    Result

    On executing, you will get the following result.

    Create Winners

    Returning Properties

    You can also return properties using the RETURN clause.

    Syntax

    Following is a syntax to return properties using the RETURN clause.

    Match (node:label {properties . . . . . . . . . . })
    Return node.property
    

    Example

    Following is a sample Cypher Query to return the properties of a node.

    Match (Dhoni:player {name: "MahendraSingh Dhoni", YOB: 1981, POB: "Ranchi"})
    Return Dhoni.name, Dhoni.POB
    

    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.

    Browser App

    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.

    Property Return

    Result

    On executing, you will get the following result.

    Streaming

    Returning All Elements

    You can return all the elements in the Neo4j database using the RETURN clause.

    Example

    Following is an example Cypher Query to return all the elements in the database.

    Match p = (n {name: "India", result: "Winners"})-[r]-(x)
    RETURN *
    

    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.

    Browser App

    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.

    All Elements

    Result

    On executing, you will get the following result.

    All Elements Result

    Returning a Variable With a Column Alias

    You can return a particular column with alias using RETURN clause in Neo4j.

    Example

    Following is a sample Cypher Query which returns the column POB as Place Of Birth.

    Match (Dhoni:player {name: "MahendraSingh Dhoni", YOB: 1981, POB: "Ranchi"})
    Return Dhoni.POB as Place Of Birth
    

    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.

    Browser App

    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 Column

    Result

    On executing, you will get the following result.

    Column Alias

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí Neo4j – Order By Clause nhận dự án làm có lương

    Neo4j – Order By Clause



    You can arrange the result data in order using the ORDER BY clause.

    Syntax

    Following is the syntax of the ORDER BY clause.

    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.

    CREATE(Dhawan:player{name:"shikar Dhawan", YOB: 1985, runs:363, country: "India"})
    CREATE(Jonathan:player{name:"Jonathan Trott", YOB:1981, runs:229, country:"South Africa"})
    CREATE(Sangakkara:player{name:"Kumar Sangakkara", YOB:1977, runs:222, country:"Srilanka"})
    CREATE(Rohit:player{name:"Rohit Sharma", YOB: 1987, runs:177, country:"India"})
    CREATE(Virat:player{name:"Virat Kohli", YOB: 1988, runs:176, country:"India"})
    

    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
    

    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.

    Browser App

    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 Name

    Result

    On executing, you will get the following result.

    Records

    Ordering Nodes by Multiple Properties

    You can arrange the nodes based on multiple properties using ORDEYBY clause.

    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
    

    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.

    Browser App

    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.

    Order By Runs

    Result

    On executing, you will get the following result.

    Ordering Nodes

    Ordering Nodes by Descending Order

    You can arrange the nodes in a database in a descending order using the ORDERBY clause.

    Syntax

    Following is the syntax to arrange the nodes in a database.

    MATCH (n)
    RETURN n
    ORDER BY n.name DESC
    

    Example

    Following is a sample Cypher Query which arranges the nodes in a database in a descending order using the ORDERBY clause.

    MATCH (n)
    RETURN n.name, n.runs
    ORDER BY n.runs DESC
    

    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.

    Browser App

    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.

    Descending Order

    Result

    On executing, you will get the following result.

    Order By

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí Neo4j – Where Clause nhận dự án làm có lương

    Neo4j – Where Clause



    Like SQL, Neo4j CQL has provided WHERE clause in CQL MATCH command to filter the results of a MATCH Query.

    Syntax

    Following is the syntax of the WHERE clause.

    MATCH (label)
    WHERE label.country = "property"
    RETURN label
    

    Example

    Before proceeding with the example, create five nodes in the database as shown below.

    CREATE(Dhawan:player{name:"shikar Dhawan", YOB: 1985, runs:363, country: "India"}
    CREATE(Jonathan:player{name:"Jonathan Trott", YOB:1981, runs:229, country:"South Africa"}
    CREATE(Sangakkara:player{name:"Kumar Sangakkara", YOB:1977, runs:222,
       country:"Srilanka"})
    CREATE(Rohit:player{name:"Rohit Sharma", YOB: 1987, runs:177, country:"India"})
    CREATE(Virat:player{name:"Virat Kohli", YOB: 1988, runs:176, country:"India"})
    CREATE(Ind:Country {name: "India", result: "Winners"})
    

    Following is a sample Cypher Query which returns all the players (nodes) that belongs to the country India using WHERE clause.

    MATCH (player)
    WHERE player.country = "India"
    RETURN player
    

    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.

    Browser App

    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.

    Where Player

    Result

    On executing, you will get the following result.

    Where Result

    WHERE Clause with Multiple Conditions

    You can also use the WHERE clause to verify multiple conditions.

    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
    

    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.

    Browser App

    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 Player

    Result

    On executing, you will get the following result.

    Condition Result

    Using Relationship with Where Clause

    You can also use Where clause to filter the nodes using the relationships.

    Example

    Assume we have the following graph in the database.

    Assumed Database

    Following is a sample Cypher Query to retrieve the top scorer of India using WHERE clause as shown below.

    MATCH (n)
    WHERE (n)-[: TOP_SCORER_OF]->( {name: "India", result: "Winners"})
    RETURN n
    

    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.

    Browser App

    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.

    Winners Result

    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.

    Returned Node

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc