Neo4j 简明教程
Neo4j - Set Clause
使用 Set 子句,可以向现有节点或关系添加新属性,也可以添加或更新现有属性值。
Using Set clause, you can add new properties to an existing Node or Relationship, and also add or update existing Properties values.
在本节中,我们将探讨如何 −
In this chapter, we are going to discuss how to −
-
Set a property
-
Remove a property
-
Set multiple properties
-
Set a label on a node
-
Set multiple labels on a node
Setting a Property
使用 SET 子句,可以在节点中创建新属性。
Using the SET clause, you can create a new property in a node.
Syntax
以下是设置属性的语法。
Following is the syntax for setting a property.
MATCH (node:label{properties . . . . . . . . . . . . . . })
SET node.property = value
RETURN node
Example
在继续案例之前,请首先像下面所示的那样创建一个名为 Dhawan 的节点。
Before proceeding with the example, first create a node named Dhawan as shown below.
CREATE (Dhawan:player{name: "shikar Dhawan", YOB: 1985, POB: "Delhi"})
下面的示例 Cypher 查询用于创建一个名为“highestscore”的属性,其值为“187”。
Following is a sample Cypher Query to create a property named “highestscore” with value “187”.
MATCH (Dhawan:player{name: "shikar Dhawan", YOB: 1985, POB: "Delhi"})
SET Dhawan.highestscore = 187
RETURN Dhawan
执行上述查询,执行以下步骤:
To execute the above query, carry out the following steps −
Step 1 - 打开 Neo4j 桌面应用程序并启动 Neo4j 服务器。使用 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 screnshot.

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.

Removing a Property
您可以通过将 NULL 作为其值传递来删除现有属性。
You can remove an existing property by passing NULL as value to it.
Syntax
以下是使用 SET 子句从节点中删除属性的语法。
Following is the syntax of removing a property from a node using the SET clause.
MATCH (node:label {properties})
SET node.property = NULL
RETURN node
Example
在继续案例之前,请首先像下面所示的那样创建一个“jadeja”节点。
Before proceeding with the example, first create a node “jadeja” as shown below.
Create (Jadeja:player {name: "Ravindra Jadeja", YOB: 1988, POB: "NavagamGhed"})
下面是一个示例 Cypher 查询,它使用 SET 子句从该节点中删除名为 POB 的属性,如下所示。
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
执行上述查询,执行以下步骤:
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.

Setting Multiple Properties
同样,您可以使用 Set 子句在节点中创建多个属性。为此,您需要用逗号指定这些键值对。
In the same way, you can create multiple properties in a node using the Set clause. To do so, you need to specify these key value pairs with commas.
Syntax
以下是使用 SET 子句在节点中创建多个属性的语法。
Following is the syntax to create multiple properties in a node using the SET clause.
MATCH (node:label {properties})
SET node.property1 = value, node.property2 = value
RETURN node
Example
下面是一个示例 Cypher 查询,它在 Neo4j 中使用 SET 子句在节点中创建多个属性。
Following is a sample Cypher Query which creates multiple properties in a node using the SET clause in Neo4j.
MATCH (Jadeja:player {name: "Ravindra Jadeja", YOB: 1988})
SET Jadeja.POB: "NavagamGhed", Jadeja.HS = "90"
RETURN Jadeja
执行上述查询,执行以下步骤:
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.

Setting a Label on a Node
您可以使用 SET 子句将标签设置为现有节点。
You can set a label to an existing node using the SET clause.
Syntax
以下是将标签设置为现有节点的语法。
Following is the syntax to set a label to an existing node.
MATCH (n {properties . . . . . . . })
SET n :label
RETURN n
Example
在继续案例之前,请首先像下面所示的那样创建一个“Anderson”节点。
Before proceeding with the example, first create a node “Anderson” as shown below.
CREATE (Anderson {name: "James Anderson", YOB: 1982, POB: "Burnely"})
以下是一个示例 Cypher 查询,用于使用 SET 子句在节点上设置标签。该查询将标签“player”添加到节点 Anderson 并返回它。
Following is a sample Cypher Query to set a label on a node using the SET clause. This query adds the label “player” to the node Anderson and returns it.
MATCH (Anderson {name: "James Anderson", YOB: 1982, POB: "Burnely"})
SET Anderson: player
RETURN Anderson
执行上述查询,执行以下步骤:
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.

Setting Multiple Labels on a Node
您可以使用 SET 子句将多个标签设置为现有节点。您需要用冒号“:”将其分隔来指定标签。
You can set multiple labels to an existing node using the SET clause. Here you need to specify the labels by separating them with colons “:”.
Syntax
以下是使用 SET 子句设置现有节点的多个标签的语法。
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
在继续执行示例之前,首先创建一个名为“Ishant”的节点,如下所示。
Before proceeding with the example, first create a node named “Ishant” as shown below.
CREATE (Ishant {name: "Ishant Sharma", YOB: 1988, POB: "Delhi"})
以下是用于使用 SET 子句对某个节点创建多个标签的 Cypher 查询示例。
Following is a sample Cypher Query used to create multiple labels on a node using the SET clause.
MATCH (Ishant {name: "Ishant Sharma", YOB: 1988, POB: "Delhi"})
SET Ishant: player:person
RETURN Ishant
执行上述查询,执行以下步骤:
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.
