Neo4j 简明教程

Neo4j - Return Clause

RETURN 子句用来返回 Neo4j 中的节点、关系和属性。在本章中,我们将学习如何 −

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

  1. Return nodes

  2. Return multiple nodes

  3. Return relationships

  4. Return properties

  5. Return all elements

  6. Return a variable with column alias

Returning Nodes

可以使用 RETURN 子句返回节点。

You can return a node using the RETURN clause.

Syntax

以下是使用 RETURN 子句返回节点的语法。

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

Create (node:label {properties})
RETURN node

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)

以下是创建一个名为 Dhoni 的节点并返回它的 Cypher 查询示例。

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 - 打开 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.

browser app

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.

return

Result

执行后,您将获得以下结果。

On executing, you will get the following result.

create player

Returning Multiple Nodes

您还可以使用 return 子句返回多个节点。

You can also return multiple nodes using the return clause.

Syntax

以下是使用 return 子句返回多个节点的语法。

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

以下是使用 return 子句返回多个节点的 Cypher 查询示例。

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 - 打开 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.

browser app

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.

multi node

Result

执行后,您将获得以下结果。在这里你可以观察到 Neo4j 返回了 2 个节点。

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

create tornament

Returning Relationships

您还可以使用 Return 子句返回关系。

You can also return relationships using the Return clause.

Syntax

以下是使用 RETURN 子句返回关系的语法。

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

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

Example

以下是一个创建两个关系并返回它们的 Cypher 查询示例。

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 - 打开 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.

browser app

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.

relationship return

Result

执行后,您将获得以下结果。

On executing, you will get the following result.

create winners

Returning Properties

您还可以使用 RETURN 子句返回属性。

You can also return properties using the RETURN clause.

Syntax

以下是使用 RETURN 子句返回属性的语法。

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

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

Example

以下是返回节点属性的 Cypher 查询示例。

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 - 打开 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.

browser app

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.

property return

Result

执行后,您将获得以下结果。

On executing, you will get the following result.

streaming

Returning All Elements

您可以使用 RETURN 子句返回 Neo4j 数据库中的所有元素。

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

Example

以下是返回数据库中所有元素的 Cypher 查询示例。

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 - 打开 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.

browser app

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.

all elements

Result

执行后,您将获得以下结果。

On executing, you will get the following result.

all elements result

Returning a Variable With a Column Alias

您可以在 Neo4j 中使用 RETURN 子句使用别名返回特定列。

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

Example

以下是一个将 POB 列作为出生地的 Cypher 查询示例。

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 - 打开 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.

browser app

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.

return column

Result

执行后,您将获得以下结果。

On executing, you will get the following result.

column alias