Neo4j 简明教程
Neo4j - Match Clause
在本章中,我们将了解 Match 子句以及可以用此子句执行的所有函数。
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
使用 Neo4j 的 MATCH 子句,可以检索 Neo4j 数据库中的所有节点。
Using the MATCH clause of Neo4j you can retrieve all nodes in the Neo4j database.
Example
在继续执行示例之前,请创建 3 个节点和 2 个关系,如下所示。
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)
以下查询返回 Neo4j 数据库中的所有节点。
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 - 打开 Neo4j Desktop App 并启动 Neo4j Server。使用 URL http://localhost:7474/ 打开 Neo4j 的内置浏览器应用,如下面的屏幕截图所示。
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 - 在美元提示符中复制并粘贴所需的查询,并按播放按钮(执行查询)突出显示在下面的屏幕截图中。
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.

Getting All Nodes Under a Specific Label
使用 Match 子句,您可以获取特定标签下的所有节点。
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
下面是一个返回数据库中带有标签 player 的所有节点的示例 Cypher 查询。
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 - 打开 Neo4j Desktop App 并启动 Neo4j Server。使用 URL http://localhost:7474/ 打开 Neo4j 的内置浏览器应用,如下面的屏幕截图所示。
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 - 在美元提示符中复制并粘贴所需的查询,并按播放按钮(执行查询)突出显示在下面的屏幕截图中。
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 by Relationship
您可以使用 MATCH 子句基于关系检索节点。
You can retrieve nodes based on relationship using the MATCH clause.
Syntax
以下是使用 MATCH 子句根据关系检索节点的语法。
Following is the syntax of retrieving nodes based on the relationship using the MATCH clause.
MATCH (node:label)<-[: Relationship]-(n)
RETURN n
Example
以下是一个基于关系使用 MATCH 子句来检索节点的示例 Cypher 查询。
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 - 打开 Neo4j Desktop App 并启动 Neo4j Server。使用 URL http://localhost:7474/ 打开 Neo4j 的内置浏览器应用,如下面的屏幕截图所示。
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 - 在美元提示符中复制并粘贴所需的查询,并按播放按钮(执行查询)突出显示在下面的屏幕截图中。
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.

Delete All Nodes
您可以使用 MATCH 子句删除所有节点。
You can delete all the nodes using the MATCH clause.
Query
以下是删除 Neo4j 中所有节点的查询。
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 - 打开 Neo4j Desktop App 并启动 Neo4j Server。使用 URL http://localhost:7474/ 打开 Neo4j 的内置浏览器应用,如下面的屏幕截图所示。
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 - 在美元提示符中复制并粘贴所需的查询,并按播放按钮(执行查询)突出显示在下面的屏幕截图中。
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.
