Neo4j 简明教程
Neo4j - Overview
Neo4j 是全球领先的开源图数据库,使用 Java 技术开发。它具有高度的可扩展性和无模式(NoSQL)。
Neo4j is the world’s leading open source Graph Database which is developed using Java technology. It is highly scalable and schema free (NoSQL).
What is a Graph Database?
图是对象集的直观表示,其中一些对象对通过链接连接。它由两个元素组成——节点(顶点)和关系(边)。
A graph is a pictorial representation of a set of objects where some pairs of objects are connected by links. It is composed of two elements - nodes (vertices) and relationships (edges).
图形数据库是一个用于以图形形式对数据建模的数据库。在此,图的节点描绘实体,而关系描绘这些节点的关联。
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.
Popular Graph Databases
Neo4j 是一个流行的图形数据库。其他图形数据库包括 Oracle NoSQL 数据库、OrientDB、HypherGraphDB、GraphBase、InfiniteGraph 和 AllegroGraph。
Neo4j is a popular Graph Database. Other Graph Databases are Oracle NoSQL Database, OrientDB, HypherGraphDB, GraphBase, InfiniteGraph, and AllegroGraph.
Why Graph Databases?
如今,大多数数据是以不同对象之间关系的形式存在的,更常见的是,数据之间的关系比数据本身更有价值。
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.
图形数据库的数据模型比其他数据库简单,并且它们可与 OLTP 系统一起使用。它们提供事务完整性和操作可用性等功能。
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.
Advantages of Neo4j
以下是 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.
-
High availability − Neo4j is highly available for large enterprise real-time applications with transactional guarantees.
-
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.
-
Cypher query language − Neo4j provides a declarative query language to represent the graph visually, using an ascii-art syntax. The commands of this language are in human readable format and very easy to learn.
-
No joins − Using Neo4j, it does NOT require complex joins to retrieve connected/related data as it is very easy to retrieve its adjacent node or relationship details without joins or indexes.
Features of Neo4j
以下是 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.
-
ACID properties − Neo4j supports full ACID (Atomicity, Consistency, Isolation, and Durability) rules.
-
Scalability and reliability − You can scale the database by increasing the number of reads/writes, and the volume without effecting the query processing speed and data integrity. Neo4j also provides support for replication for data safety and reliability.
-
Cypher Query Language − Neo4j provides a powerful declarative query language known as Cypher. It uses ASCII-art for depicting graphs. Cypher is easy to learn and can be used to create and retrieve relations between data without using the complex queries like Joins.
-
Built-in web application − Neo4j provides a built-in Neo4j Browser web application. Using this, you can create and query your graph data.
-
Drivers − Neo4j can work with − REST API to work with programming languages such as Java, Spring, Scala etc. Java Script to work with UI MVC frameworks such as Node JS. It supports two kinds of Java API: Cypher API and Native Java API to develop Java applications. In addition to these, you can also work with other databases such as MongoDB, Cassandra, etc.
-
Indexing − Neo4j supports Indexes by using Apache Lucence.
Neo4j - Data Model
Neo4j Property Graph Data Model
Neo4j 图形数据库遵循属性图形模型来存储和管理其数据。
Neo4j Graph Database follows the Property Graph Model to store and manage its data.
以下是属性图形模型的关键特性 -
Following are the key features of Property Graph Model −
-
The model represents data in Nodes, Relationships and Properties
-
Properties are key-value pairs
-
Nodes are represented using circle and Relationships are represented using arrow keys
-
Relationships have directions: Unidirectional and Bidirectional
-
Each Relationship contains "Start Node" or "From Node" and "To Node" or "End Node"
-
Both Nodes and Relationships contain properties
-
Relationships connects nodes
在属性图形数据模型中,关系应该是方向性的。如果我们尝试创建没有方向的关系,那么它将抛出一个错误消息。
In Property Graph Data Model, relationships should be directional. If we try to create relationships without direction, then it will throw an error message.
在 Neo4j 中,关系也应该是方向性的。如果我们尝试创建没有方向的关系,那么 Neo4j 将抛出一个错误消息,指出“关系应该是方向性的”。
In Neo4j too, relationships should be directional. If we try to create relationships without direction, then Neo4j will throw an error message saying that "Relationships should be directional".
Neo4j 图形数据库将所有数据存储在节点和关系中。我们既不需要任何附加的 RRBMS 数据库,也不需要任何 SQL 数据库来存储 Neo4j 数据库数据。它以其本机格式以图形的形式存储其数据。
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 使用 Native GPE(图形处理引擎)来处理其 Native 图形存储格式。
Neo4j uses Native GPE (Graph Processing Engine) to work with its Native graph storage format.
图形数据库数据模型的主要构建模块有 -
The main building blocks of Graph DB Data Model are −
-
Nodes
-
Relationships
-
Properties
以下是属性图形的一个简单示例。
Following is a simple example of a Property Graph.

在这里,我们使用圆圈表示节点。关系使用箭头表示。关系是方向性的。我们可以用属性(键值对)表示节点的数据。在此示例中,我们在节点的圆圈内表示了每个节点的 ID 属性。
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.
Neo4j - Environment Setup
在本章中,我们将讨论如何使用 exe 文件在系统中安装 Neo4j。
In this chapter, we will discuss how to install Neo4j in your system using exe file.
Neo4j Database Server Setup with Windows exe File
按照下列步骤下载 Neo4j 到你的系统中。
Follow the steps given below to download Neo4j into your system.
Step 1 − 使用 https://neo4j.com/ 访问 Neo4j 官方网站。单击此链接后,你将转到 neo4j 网站的主页。
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 − 如上图所示,此页面在右上角有一个“下载”按钮。单击此按钮。
Step 2 − As highlighted in the above screenshot, this page has a Download button on the top right hand side. Click it.
Step 3 − 你将转到下载页面,你可以在其中下载 Neo4j 的社区版和企业版。单击相应的按钮来下载该软件的社区版。
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 − 你将转到一个页面,你可以在其中下载与不同操作系统兼容的 Neo4j 软件的社区版。下载与所需操作系统相应的该文件。
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.

这会将一个名为 neo4j-community_windows-x64_3_1_1.exe 的文件下载到你的系统中,如下面的截图所示。
This will download a file named neo4j-community_windows-x64_3_1_1.exe to your system as shown in the following screenshot.

Step 5 − 双击 exe 文件以安装 Neo4j Server。
Step 5 − Double-click the exe file to install Neo4j Server.

Step 6 − 接受许可证协议并继续安装。在完成此过程后,你可以看到 Neo4j 已安装在你的系统中。
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.
Starting the Server
Step 1 − 单击 Windows 开始菜单,并单击 Neo4j 的开始菜单快捷方式以启动 Neo4j 服务器。
Step 1 − Click the Windows startmenu and start the Neo4j server by clicking the start menu shortcut for Neo4j.

Step 2 - 单击快捷方式后,您将获得 Neo4j Community 版的一个窗口。默认情况下,它选择 c:\Users\[username]\Documents\Neo4j\default.graphdb。如果您愿意,可以将您的路径更改为不同的目录。
Step 2 − On clicking the shortcut, you will get a window for Neo4j Community edition. By default, it selects c:\Users\[username]\Documents\Neo4j\default.graphdb. If you want, you can change your path to a different directory.

Step 3 - 单击“启动”按钮以启动 Neo4j 服务器。
Step 3 − Click the "Start" button to start the Neo4j server.

服务器启动后,您可以观察到数据库目录已填充,如下面的屏幕截图所示。
Once the server starts, you can observe that the database directory is populated as shown in the following screenshot.

Working with Neo4j
正如前面章节中所讨论的,neo4j 提供了一个内置的浏览应用程序来与 Neo4j 配合使用。您可以使用以下网址访问 Neo4j http://localhost:7474/
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/

Neo4j - Building Blocks
Neo4j 图形数据库具有以下构件 −
Neo4j Graph Database has the following building blocks −
-
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.

在此,节点名称 =“Employee”,它包含一系列属性作为键值对。
Here, Node Name = "Employee" and it contains a set of properties as key-value pairs.
Properties
属性是键值对,用来描述图形节点和关系。
Property is a key-value pair to describe Graph Nodes and Relationships.
Key = Value
其中键是一个字符串,值可以使用任何 Neo4j 数据类型表示。
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.

这里,Emp 和 Dept 是两个不同的节点。“WORKS_FOR”是 Emp 和 Dept 节点之间的关系。
Here, Emp and Dept are two different nodes. "WORKS_FOR" is a relationship between Emp and Dept nodes.
它表示,从 Emp 到 Dept 的箭头标记描述了 -
As it denotes, the arrow mark from Emp to Dept, this relationship describes −
Emp WORKS_FOR Dept
每个关系包含一个起始节点和一个结束节点。
Each relationship contains one start node and one end node.
在这里,“Emp”是一个起始节点,“Dept”是一个结束节点。
Here, "Emp" is a start node, and "Dept" is an end node.
由于该关系箭头标记表示从“Emp”节点到“Dept”节点的关系,因此该关系被称为“Dept”节点的“流入关系”和“Emp”节点的“流出关系”。
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.
与节点一样,关系也可以包含属性作为键值对。
Like nodes, relationships also can contain properties as key-value pairs.

在这里,“WORKS_FOR”关系具有一个属性作为键值对。
Here, "WORKS_FOR" relationship has one property as key-value pair.
Id = 123
它表示该关系的 ID。
It represents an Id of this relationship.
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.
从上一个图表中,我们可以观察到有 2 个节点。
From the previous diagram, we can observe that there are two nodes.
左侧节点具有标签:“Emp”,右侧节点具有标签:“Dept”。
Left side node has a Label: "Emp" and the right side node has a Label: "Dept".
这两个节点之间的关系也具有标签:“WORKS_FOR”。
Relationship between those two nodes also has a Label: "WORKS_FOR".
Note − Neo4j 将数据存储在节点或关系的属性中。
Note − Neo4j stores data in Properties of Nodes or Relationships.
Neo4j Data Browser
一旦我们安装了 Neo4j,我们就可以使用以下 URL 访问 Neo4j 数据浏览器
Once we install Neo4j, we can access Neo4j Data Browser using the following URL
http://localhost:7474/browser/

Neo4j 数据浏览器用于执行 CQL 命令和查看输出。
Neo4j Data Browser is used to execute CQL commands and view the output.
在这里,我们需要在美元提示符下执行所有 CQL 命令: "$"
Here, we need to execute all CQL commands at dollar prompt: "$"
在美元符号后键入命令,然后单击“执行”按钮来运行你的命令。
Type commands after the dollar symbol and click the "Execute" button to run your commands.
它与 Neo4j 数据库服务器交互,检索并将结果显示在美元提示符的正下方。
It interacts with Neo4j Database Server, retrieves and displays the results just below the dollar prompt.
使用“VI 视图”按钮以图表格式查看结果。上图显示了“UI 视图”格式下的结果。
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.
Neo4j CQL - Introduction
CQL 是 Cypher 查询语言的简称。就像 Oracle Database 具有 SQL 查询语言一样,Neo4j 具有 CQL 作为查询语言。
CQL stands for Cypher Query Language. Like Oracle Database has query language SQL, Neo4j has CQL as query language.
Neo4j CQL
-
Is a query language for Neo4j Graph Database.
-
Is a declarative pattern-matching language.
-
Follows SQL like syntax.
-
Syntax is very simple and in human readable format.
Like Oracle SQL
-
Neo4j CQL has commands to perform Database operations.
-
Neo4j CQL supports many clauses such as WHERE, ORDER BY, etc., to write very complex queries in an easy manner.
-
Neo4j CQL supports some functions such as String, Aggregation. In addition to them, it also supports some Relationship Functions.
Neo4j CQL Clauses
以下是 Neo4j *C*ypher *Q*uery *L*anguage 的读取子句:
Following are the read clauses of Neo4j *C*ypher *Q*uery *L*anguage −
Sr.No |
Read Clauses |
Usage |
1 |
MATCH |
This clause is used to search the data with a specified pattern. |
2 |
OPTIONAL MATCH |
This is the same as match, the only difference being it can use nulls in case of missing parts of the pattern. |
3 |
WHERE |
This clause id is used to add contents to the CQL queries. |
4 |
START |
This clause is used to find the starting points through the legacy indexes. |
5 |
LOAD CSV |
This clause is used to import data from CSV files. |
以下是 Neo4j *C*ypher *Q*uery *L*anguage 的写入子句:
Following are the write clauses of Neo4j *C*ypher *Q*uery *L*anguage −
Sr.No |
Write Clause |
Usage |
1 |
CREATE |
This clause is used to create nodes, relationships, and properties. |
2 |
MERGE |
This clause verifies whether the specified pattern exists in the graph. If not, it creates the pattern. |
3 |
SET |
This clause is used to update labels on nodes, properties on nodes and relationships. |
4 |
DELETE |
This clause is used to delete nodes and relationships or paths etc. from the graph. |
5 |
REMOVE |
This clause is used to remove properties and elements from nodes and relationships. |
6 |
FOREACH |
This class is used to update the data within a list. |
7 |
CREATE UNIQUE |
Using the clauses CREATE and MATCH, you can get a unique pattern by matching the existing pattern and creating the missing one. |
8 |
Importing CSV files with Cypher |
Using Load CSV you can import data from .csv files. |
以下是 Neo4j *C*ypher *Q*uery *L*anguage 的常用子句 -
Following are the general clauses of Neo4j *C*ypher *Q*uery *L*anguage −
Sr.No |
General Clauses |
Usage |
1 |
RETURN |
This clause is used to define what to include in the query result set. |
2 |
ORDER BY |
This clause is used to arrange the output of a query in order. It is used along with the clauses RETURN or WITH. |
3 |
LIMIT |
This clause is used to limit the rows in the result to a specific value. |
4 |
SKIP |
This clause is used to define from which row to start including the rows in the output. |
5 |
WITH |
This clause is used to chain the query parts together. |
6 |
UNWIND |
This clause is used to expand a list into a sequence of rows. |
7 |
UNION |
This clause is used to combine the result of multiple queries. |
8 |
CALL |
This clause is used to invoke a procedure deployed in the database. |
Neo4j CQL Functions
以下是常用的 Neo4j CQL 函数 -
Following are the frequently used Neo4j CQL Functions −
Sr.No |
CQL Functions |
Usage |
1 |
String |
They are used to work with String literals. |
2 |
Aggregation |
They are used to perform some aggregation operations on CQL Query results. |
3 |
Relationship |
They are used to get details of relationships such as startnode, endnode, etc. |
在后续章节中,我们将详细讨论所有 Neo4j CQL 命令、子句和函数的语法、用法和示例。
We will discuss all Neo4j CQL commands, clauses and functions syntax, usage and examples in-detail in the subsequent chapters.
Neo4j CQL Data Types
这些数据类型类似于 Java 语言。它们用于定义节点或关系的属性。
These data types are similar to Java language. They are used to define properties of a node or a relationship.
Neo4j CQL 支持以下数据类型 −
Neo4j CQL supports the following data types −
Sr.No |
CQL Data Type |
Usage |
1 |
Boolean |
It is used to represent Boolean literals: true, false. |
2 |
byte |
It is used to represent 8-bit integers. |
3 |
short |
It is used to represent 16-bit integers. |
4 |
int |
It is used to represent 32-bit integers. |
5 |
long |
It is used to represent 64-bit integers. |
6 |
float |
It is used to represent 32-bit floating-point numbers. |
7 |
double |
It is used to represent 64-bit floating-point numbers. |
8 |
char |
It is used to represent 16-bit characters. |
9 |
String |
It is used to represent Strings. |
CQL Operators
以下列出了 Neo4j Cypher 查询语言支持的操作符。
Following are the list of operators supported by Neo4j Cypher Query language.
Sr.No |
Type |
Operators |
1 |
Mathematical |
+, -, *, /, %, ^ |
2 |
Comparison |
+, <>, <, >, ⇐, >= |
3 |
Boolean |
AND, OR, XOR, NOT |
4 |
String |
+ |
5 |
List |
+, IN, [X], [X…..Y] |
6 |
Regular Expression |
=- |
7 |
String matching |
STARTS WITH, ENDS WITH, CONSTRAINTS |
Boolean Operators in Neo4j CQL
Neo4j 支持使用 Neo4j CQL WHERE 子句中的以下布尔运算符来支持多个条件。
Neo4j supports the following Boolean operators to use in Neo4j CQL WHERE clause to support multiple conditions.
Sr.No |
Boolean Operators |
Description |
1 |
AND |
It is a Neo4j CQL keyword to support AND operation. It is like SQL AND operator. |
2 |
OR |
It is a Neo4j CQL keyword to support OR operation. It is like SQL AND operator. |
3 |
NOT |
It is a Neo4j CQL keyword to support NOT operation. It is like SQL AND operator. |
4 |
XOR |
It is a Neo4j CQL keyword to support XOR operation. It is like SQL AND operator. |
Comparison Operators in Neo4j CQL
Neo4j 支持在 Neo4j CQL WHERE 子句中使用以下比较运算符,以支持条件。
Neo4j supports the following Comparison operators to use in Neo4j CQL WHERE clause to support conditions.
Sr.No |
Boolean Operators |
Description |
1 |
= |
It is a Neo4j CQL "Equal To" operator. |
2 |
< |
It is a Neo4j CQL "Not Equal To" operator. |
3 |
< |
It is a Neo4j CQL "Less Than" operator. |
4 |
> |
It is a Neo4j CQL "Greater Than" operator. |
5 |
⇐ |
It is a Neo4j CQL "Less Than Or Equal To" operator. |
6 |
> = |
It is a Neo4j CQL "Greater Than Or Equal To" operator. |
Neo4j CQL - Creating Nodes
正如讨论的那样,一个节点是图数据库中的一个数据/记录。你可以使用 CREATE 从句在 Neo4j 中创建一个节点。本章教你如何 -
As discussed, a node is a data/record in a graph database. You can create a node in Neo4j using the CREATE clause. This chapter teaches you how to −
-
Create a single node
-
Create multiple nodes
-
Create a node with a label
-
Create a node with multiple labels
-
Create a node with properties
-
Returning the created node
Creating a Single node
你可以简单地通过指定要创建的节点名称,并使用 CREATE 从句来在 Neo4j 中创建一个节点。
You can create a node in Neo4j by simply specifying the name of the node that is to be created along with the CREATE clause.
Syntax
以下是使用 Cypher 查询语言创建节点的语法。
Following is the syntax for creating a node using Cypher Query Language.
CREATE (node_name);
Note - 分号 (;) 是可选的。
Note − Semicolon (;) is optional.
Example
以下是一个示例 Cypher 查询,在 Neo4j 中创建了一个节点。
Following is a sample Cypher Query which creates a node in Neo4j.
CREATE (sample)
执行上述查询,执行以下步骤:
To execute the above query, carry out the following steps −
Step 1 - 打开 Neo4j 桌面应用程序,并启动 Neo4j 服务器,如下图所示。
Step 1 − Open the Neo4j desktop App and start the Neo4j Server as shown in the following screenshot.

Step 2 - 打开你的浏览器,复制代码并粘贴下面的 URL 到你的地址栏 http://localhost:7474/. 这将给你 Neo4j 的内建浏览器应用程序,其中有个美元提示符,如下图所示。
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 - 将目标查询复制并粘贴到美元提示符中,然后按下在以下截图中突出显示的播放按钮(执行查询)。
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.

Verification
要验证节点类型的创建,在美元提示符中执行以下查询。
To verify the creation of the node type, 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.

Creating Multiple Nodes
Neo4j CQL 的 create 子句也用于同时创建多个节点。为此,你需要传递要创建的节点的名称,用逗号分隔。
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
以下是使用 CREATE 从句创建多个节点的语法。
Following is the syntax to create multiple nodes using the CREATE clause.
CREATE (node1),(node2)
Example
以下是一个示例 Cypher 查询,在 Neo4j 中创建多个节点。
Following is a sample Cypher Query which creates multiple nodes in Neo4j.
CREATE (sample1),(sample2)
执行上述查询,执行以下步骤:
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.

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.

Creating a Node with a Label
Neo4j 中的标签用于使用标签对节点进行分组(分类)。你可以使用 CREATE 从句为 Neo4j 中的节点创建一个标签。
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
以下是使用 Cypher 查询语言用标签创建节点的语法。
Following is the syntax for creating a node with a label using Cypher Query Language.
CREATE (node:label)
Example
以下是一个创建带有标签的节点的示例 Cypher 查询。
Following is a sample Cypher Query which creates a node with a label.
CREATE (Dhawan:player)
执行上述查询,执行以下步骤:
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.

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.

Creating a Node with Multiple Labels
还可以为单个节点创建多个标签。需要通过用冒号“ : ”分隔标签来指定节点的标签。
You can also create multiple labels for a single node. You need to specify the labels for the node by separating them with a colon “ : ”.
Syntax
以下是创建具有多个标签的节点的语法。
Following is the syntax to create a node with multiple labels.
CREATE (node:label1:label2:. . . . labeln)
Example
以下是一个在 Neo4j 中创建带有多个标签的节点的示例 Cypher 查询。
Following is a sample Cypher Query which creates a node with multiple labels in Neo4j.
CREATE (Dhawan:person:player)
执行上述查询,执行以下步骤:
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.

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.

Create Node with Properties
属性是节点用于存储数据 key-value 对。可以使用 CREATE 子句创建带有属性的节点。需要在花括号“ { }”中用逗号分隔指定这些属性。
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
以下是创建带有属性的节点的语法。
Following is the syntax to create a node with properties.
CREATE (node:label { key1: value, key2: value, . . . . . . . . . })
Example
以下是一个创建带有属性的节点的示例 Cypher 查询。
Following is a sample Cypher Query which creates a node with properties.
CREATE (Dhawan:player{name: "Shikar Dhawan", YOB: 1985, POB: "Delhi"})
执行上述查询,执行以下步骤:
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.

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.

Returning the Created Node
在整个章节中,我们使用 MATCH (n) RETURN n 查询来查看创建的节点。此查询返回数据库中所有现有的节点。
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.
我们能够使用 RETURN 子句和 CREATE 来代替上述方法来查看新创建的节点。
Instead of this, we can use the RETURN clause with CREATE to view the newly created node.
Syntax
以下是返回 Neo4j 中节点的语法。
Following is the syntax to return a node in Neo4j.
CREATE (Node:Label{properties. . . . }) RETURN Node
Example
以下是一个创建带有属性并返回该属性的节点的示例 Cypher 查询。
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 Dhawan
执行上述查询,执行以下步骤:
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.

Neo4j CQL - Creating a Relationship
在 Noe4j 中,关系是一个元素,我们使用它来连接图中的两个节点。这些关系具有方向、类型和数据形式模式。本章将介绍如何:
In Noe4j, a relationship is an element using which we connect two nodes of a graph. These relationships have direction, type, and the form patterns of data. This chapter teaches you how to −
-
Create relationships
-
Create a relationship between the existing nodes
-
Create a relationship with label and properties
Creating Relationships
我们可以使用 CREATE 子句创建一个关系。我们将根据关系的方向在方括号 “[ ]” 中指定关系,因为它是放在连字符 “ - ” 和箭头 “ → ” 之间的,如下所示语法中所示。
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 子句创建关系的语法。
Following is the syntax to create a relationship using the CREATE clause.
CREATE (node1)-[:RelationshipType]->(node2)
Example
首先,在数据库中创建两个节点 Ind 和 Dhawan,如下所示。
First of all, create two nodes Ind and Dhawan in the database, as shown below.
CREATE (Dhawan:player{name: "Shikar Dhawan", YOB: 1985, POB: "Delhi"})
CREATE (Ind:Country {name: "India"})
现在,在两个节点之间创建一个名为 BATSMAN_OF 的关系,如下所示:
Now, create a relationship named BATSMAN_OF between these two nodes as −
CREATE (Dhawan)-[r:BATSMAN_OF]->(Ind)
最后,返回两个节点以查看已创建的关系。
Finally, return both the nodes to see the created relationship.
RETURN Dhawan, Ind

将所需查询复制并粘贴到美元提示符中,然后按下以下屏幕截图中突出显示的播放按钮(以执行查询)。
Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in the following screenshot.

Creating a Relationship Between the Existing Nodes
你还可以使用 MATCH 子句在现有节点之间创建关系。
You can also create a relationship between the existing nodes using the MATCH clause.
Syntax
以下是使用 MATCH 子句创建关系的语法。
Following is the syntax to create a relationship using the MATCH clause.
MATCH (a:LabeofNode1), (b:LabeofNode2)
WHERE a.name = "nameofnode1" AND b.name = " nameofnode2"
CREATE (a)-[: Relation]->(b)
RETURN a,b
Example
以下就是一个使用 match 子句创建关系的 Cypher 查询示例。
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 = "India"
CREATE (a)-[r: BATSMAN_OF]->(b)
RETURN a,b
要执行以上查询,请执行以下步骤。
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.

Creating a Relationship with Label and Properties
可以使用 CREATE 子句创建带有标签和属性的关系。
You can create a relationship with label and properties using the CREATE clause.
Syntax
下面是使用 CREATE 子句创建带有标签和属性的关系的语法。
Following is the syntax to create a relationship with label and properties using the CREATE clause.
CREATE (node1)-[label:Rel_Type {key1:value1, key2:value2, . . . n}]-> (node2)
Example
下面是一个示例 Cypher 查询,它创建了一个带有标签和属性的关系。
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 = "India"
CREATE (a)-[r:BATSMAN_OF {Matches:5, Avg:90.75}]->(b)
RETURN a,b
执行上述查询,执行以下步骤:
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.

Creating a Complete Path
在 Neo4j 中,关系连续使用路径进行形成。可以使用 create 子句创建路径。
In Neo4j, a path is formed using continuous relationships. A path can be created using the create clause.
Syntax
下面是使用 CREATE 子句在 Neo4j 中创建路径的语法。
Following is the syntax to create a path in Neo4j using the CREATE clause.
CREATE p = (Node1 {properties})-[:Relationship_Type]->
(Node2 {properties})[:Relationship_Type]->(Node3 {properties})
RETURN p
Example
执行上述查询,执行以下步骤:
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.

Neo4j - Merge Command
MERGE 命令是 CREATE 命令和 MATCH 命令的组合。
MERGE command is a combination of CREATE command and MATCH command.
Neo4j CQL MERGE 命令在图中搜索给定的模式。如果存在,则它返回结果。
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.
在本章中,你将学习如何 −
In this chapter you are going to learn how to −
-
Merge a node with label
-
Merge a node with properties
-
OnCreate and OnMatch
-
Merge a relationship
Syntax
下面是 MERGE 命令的语法。
Following is the syntax for the MERGE command.
MERGE (node: label {properties . . . . . . . })
在继续本节中的示例之前,请在数据库中创建两个带有标签 Dhawan 和 Ind 的节点。创建从 Dhawan 到 Ind 的类型为 “BATSMAN_OF” 的关系,如下所示。
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.
CREATE (Dhawan:player{name: "Shikar Dhawan", YOB: 1985, POB: "Delhi"})
CREATE (Ind:Country {name: "India"})
CREATE (Dhawan)-[r:BATSMAN_OF]->(Ind)
Merging a Node with a Label
可以使用 MERGE 子句根据标签合并数据库中的节点。如果尝试根据标签合并节点,则 Neo4j 会验证是否存在带有给定标签的任何节点。如果没有,将创建当前节点。
You can merge a node in the database based on the label using the MERGE clause. If you try to merge a node based on the label, then Neo4j verifies whether there exists any node with the given label. If not, the current node will be created.
Syntax
以下是根据标签合并节点的语法。
Following is the syntax to merge a node based on a label.
MERGE (node:label) RETURN node
Example 1
下面是一个 Cypher 查询示例,它将一个节点合并到 Neo4j 中(根据标签)。执行此查询时,Neo4j 会验证是否存在任何标签为 player 的节点。如果没有,它会创建一个名为 “Jadeja” 的节点并返回它。
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.
如果存在任何带有给定标签的节点,Neo4j 会返回它们全部。
If, there exists any node with the given label, Neo4j returns them all.
MERGE (Jadeja:player) 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.

Result
执行后,你将获得以下结果。由于你已在数据库中创建了一个标签为 “player” 的名为 “Dhawan” 的节点,所以 Neo4j 会按以下屏幕截图所示返回它。
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
现在,尝试合并一个名为 “CT2013” 的节点,其标签名为 Tournament。由于没有带有此标签的节点,Neo4j 会创建一个带有给定名称的节点并返回它。
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.
MERGE (CT2013:Tournament{name: "ICC Champions Trophy 2013"})
RETURN CT2013, labels(CT2013)
执行上述查询,执行以下步骤:
To execute the above query, carry out the following steps −
Step 1 - 打开 Neo4j 桌面应用程序并启动 Neo4j 服务器。使用 URL 打开 Neo4j 的内置浏览器应用程序 http://localhost:7474/ ,如下图所示。
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.

Merging a Node with Properties
您还可以使用一组属性合并节点。如果您这样做,Neo4j 将搜索与指定节点相等的匹配项,包括属性。如果找不到任何匹配项,则创建一个匹配项。
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
以下是使用属性合并节点的语法。
Following is the syntax to merge a node using properties.
MERGE (node:label {key1:value, key2:value, key3:value . . . . . . . . })
Example
以下是一个示例 Cypher 查询,用于使用属性合并节点。此查询尝试使用属性和标签合并名为 “jadeja” 的节点。由于没有具有确切标签和属性的此类节点,因此 Neo4j 创建了一个。
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.
MERGE (Jadeja:player {name: "Ravindra Jadeja", YOB: 1988, POB: "NavagamGhed"})
RETURN Jadeja
执行上述查询,执行以下步骤:
To execute the above query, carry out the following steps −
Step 1 - 打开 Neo4j 桌面应用程序并启动 Neo4j 服务器。使用 URL 打开 Neo4j 的内置浏览器应用程序 http://localhost:7474/ ,如下图所示。
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.

OnCreate and OnMatch
每当我们执行合并查询时,一个节点要么匹配,要么创建。使用 on create 和 on match,您可以设置属性来指示节点是创建还是匹配。
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
以下是 OnCreate 和 OnMatch 子句的语法。
Following is the syntax of OnCreate and OnMatch clauses.
MERGE (node:label {properties . . . . . . . . . . .})
ON CREATE SET property.isCreated ="true"
ON MATCH SET property.isFound ="true"
Example
以下是一个示例 Cypher 查询,演示了在 Neo4j 中使用 OnCreate 和 OnMatch 子句。如果指定的节点已存在于数据库中,则将匹配该节点并在该节点中创建具有键值对 isFound = "true" 的属性。
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.
如果指定的节点不存在于数据库中,则将创建该节点,并在其中创建具有键值对 isCreated ="true" 的属性。
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.
MERGE (Jadeja:player {name: "Ravindra Jadeja", YOB: 1988, POB: "NavagamGhed"})
ON CREATE SET Jadeja.isCreated = "true"
ON MATCH SET Jadeja.isFound = "true"
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.

Merge a Relationship
就像节点一样,您还可以使用 MERGE 子句合并关系。
Just like nodes, you can also merge the relationships using the MERGE clause.
Example
以下是一个示例 Cypher 查询,它使用 Neo4j 中的 MATCH 子句合并关系。此查询尝试合并节点 “ind”(标签:国家和名称:印度)和 ICC13(标签:锦标赛和名称:ICC 冠军奖杯 2013)之间的名为 WINNERS_OF 的关系。
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).
由于不存在此类关系,因此 Neo4j 创建了一个。
Since such relation doesn’t exist, Neo4j creates one.
MATCH (a:Country), (b:Tournament)
WHERE a.name = "India" AND b.name = "ICC Champions Trophy 2013"
MERGE (a)-[r:WINNERS_OF]->(b)
RETURN a, b
执行上述查询,执行以下步骤:
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.

Result
在执行时,您将获得以下结果。由于指定的关联不存在于数据库中,因此 Neo4j 会创建一个,如下图所示。
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.
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.

Neo4j - Delete Clause
你可以使用 DELETE 子句从数据库删除节点和关系。
You can delete nodes and relationships from a database using the DELETE clause.
Deleting All Nodes and Relationships
以下是使用 DELETE 子句从数据库删除所有节点和关系的查询:
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 - 打开 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.

这将删除 neo4j 数据库中的所有节点和关系并使其变空。
This will delete all the nodes and relationships from your neo4j database and make it empty.
Deleting a Particular Node
要删除具体节点,您需要在以上查询中将其详细信息指定在“n”的位置。
To delete a particular node, you need to specify the details of the node in the place of “n” in the above query.
Syntax
以下是使用 DELETE 子句从 Neo4j 中删除具体节点的语法。
Following is the syntax to delete a particular node from Neo4j using the DELETE clause.
MATCH (node:label {properties . . . . . . . . . . })
DETACH DELETE node
Example
在继续举例之前,请按照以下指示在 Neo4j 数据库中创建一个名为“Ishant”的节点。
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"})
以下是一个使用 DELETE 子句删除上述创建节点的示例 Cypher 查询。
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 - 打开 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.

Neo4j - Remove Clause
REMOVE 子句用于从图元素(节点或关系)中移除属性和标签。
The REMOVE clause is used to remove properties and labels from graph elements (Nodes or Relationships).
Neo4j CQL DELETE 和 REMOVE 命令之间的主要区别是 −
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
你可以使用 MATCH 和 REMOVE 子句来删除节点的属性。
You can remove a property of a node using MATCH along with the REMOVE clause.
Syntax
以下是使用 REMOVE 子句删除节点属性的语法。
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
在继续执行示例之前,创建一个名为 Dhoni 的节点,如下所示。
Before proceeding with the example, create a node named Dhoni as shown below.
CREATE (Dhoni:player {name: "MahendraSingh Dhoni", YOB: 1981, POB: "Ranchi"})
以下是使用 REMOVE 子句删除以上创建的节点的示例 Cypher 查询。
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 - 打开 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.

Removing a Label From a Node
类似于属性,你也可以使用 remove 子句从现有节点中删除标签。
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
以下是使用 remove 子句从现有节点中删除标签的示例 Cypher 查询。
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 - 打开 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.

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
在继续执行示例之前,创建一个称为 Ishant 的节点,如下所示。
Before proceeding with the example, create a node Ishant as shown below.
CREATE (Ishant:player:person {name: "Ishant Sharma", YOB: 1988, POB: "Delhi"})
以下是从节点中删除多个标签的示例 Cypher 查询。
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 - 打开 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.

Neo4j - Foreach Clause
FOREACH 子句用于更新列表中的数据,无论这些数据是路径的组件还是聚合的结果。
The FOREACH clause is used to update data within a list whether components of a path, or result of aggregation.
Syntax
以下是 FOREACH 子句的语法。
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
在继续示例之前,请按如下所示在 Neo4j 数据库中创建一个路径 p 。
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
以下是一个使用 FOREACH 子句为路径中的所有节点添加属性的示例 Cypher 查询。
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 - 打开 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.

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.

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.

Neo4j - Optional Match Clause
OPTIONAL MATCH 子句用于搜索其中描述的模式,同时对模式的缺失部分使用 null。
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 与 match 子句相似,唯一的区别是它返回 null 作为模式缺失部分的结果。
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
以下是 OPTIONAL MATCH 与关系的语法。
Following is the syntax of the OPTIONAL MATCH with relationship.
MATCH (node:label {properties. . . . . . . . . . . . . .})
OPTIONAL MATCH (node)-->(x)
RETURN x
Example
以下就是一个尝试从节点 ICCT2013 中检索关系的 Cypher 查询示例。由于没有这样的节点,所以它返回 null。
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 - 打开 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.

Neo4j - Where Clause
与 SQL 类似,Neo4j CQL 在 CQL MATCH 命令中提供了 WHERE 子句以过滤 MATCH 查询的结果。
Like SQL, Neo4j CQL has provided WHERE clause in CQL MATCH command to filter the results of a MATCH Query.
Syntax
以下是 WHERE 子句的语法。
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"})
以下是使用 WHERE 子句返回所有属于印度的球员(节点)的示例 Cypher 查询。
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 - 打开 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.

WHERE Clause with Multiple Conditions
您还可以使用 WHERE 子句验证多个条件。
You can also use the WHERE clause to verify multiple conditions.
Syntax
以下是具有多个条件在 Neo4j 中使用 WHERE 子句的语法。
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
以下是使用两种条件在 Neo4j 数据库中过滤节点的示例 Cypher 查询。
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 - 打开 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.

Using Relationship with Where Clause
您还可以使用 Where 子句使用关系来过滤节点。
You can also use Where clause to filter the nodes using the relationships.
Example
假设我们有数据库中的以下图表。
Assume we have the following graph in the database.

以下是如下所示使用 WHERE 子句检索印度最高得分的示例 Cypher 查询。
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 - 打开 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.

Neo4j - Count Function
假设我们在数据库中创建了具有以下详细信息的图表。
Assume we have created a graph in the database with the following details.

Count
count() 函数用于统计行数。
The count() function is used to count the number of rows.
Syntax
如下是计数函数的语法。
Following is the syntax of the count function.
MATCH (n { name: 'A' })-->(x)
RETURN n, count(*)
Example
以下是演示 count() 函数用法的一个 Cypher 查询示例。
Following is a sample Cypher Query which demonstrates the usage of the count() function.
Match(n{name: "India", result: "Winners"})--(x)
RETURN n, count(*)
执行上述查询,执行以下步骤:
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.

Group Count
COUNT 子句还用于统计关系类型组。
The COUNT clause is also used to count the groups of relationship types.
Example
以下是统计参与每个关系的节点的数量并返回的 Cypher 查询示例。
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(*)
执行上述查询,执行以下步骤:
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.

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 −
-
Return nodes
-
Return multiple nodes
-
Return relationships
-
Return properties
-
Return all elements
-
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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

Neo4j - Order By Clause
您可以使用 ORDER BY 子句按顺序排列结果数据。
You can arrange the result data in order using the ORDER BY clause.
Syntax
以下是 ORDER BY 从句的语法。
Following is the syntax of the ORDER BY clause.
MATCH (n)
RETURN n.property1, n.property2 . . . . . . . .
ORDER BY n.property
Example
在继续举例之前,请按照以下指示在 Neo4j 数据库中创建 5 个节点。
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"})
以下是一个使用 ORDERBY 子句按球员得分顺序返回上述创建节点的示例 Cypher 查询。
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 - 打开 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.

Ordering Nodes by Multiple Properties
您可以使用 ORDEYBY 子句按多个属性整理节点。
You can arrange the nodes based on multiple properties using ORDEYBY clause.
Syntax
以下是使用 ORDERBY 子句按多个属性整理节点的语法。
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
以下是一个示例 Cypher 查询,该查询按以下属性对本章前面创建的节点进行整理:得分和国家。
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 - 打开 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.

Ordering Nodes by Descending Order
您可以使用 ORDERBY 子句按降序排列数据库中的节点。
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
以下是一个示例 Cypher 查询,该查询使用 ORDERBY 子句按降序排列数据库中的节点。
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 - 打开 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.

Neo4j - Limit Clause
limit 子句用于限制输出中的行数。
The limit clause is used to limit the number of rows in the output.
Syntax
以下为 LIMIT 子句的语法。
Following is the syntax of the LIMIT clause.
MATCH (n)
RETURN n
ORDER BY n.name
LIMIT 3
Example
在继续举例之前,请按照以下指示在 Neo4j 数据库中创建 5 个节点。
Before proceeding with the example, create 5 nodes in the 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"})
以下是一个示例 Cypher 查询,该查询按降序返回上述创建的节点,并将其结果中的记录限制为 3 个。
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
执行上述查询,执行以下步骤:
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.

Limit with expression
您还可以对表达式使用 LIMIT 子句。
You can also use the LIMIT clause with expression.
Example
以下是限制使用表达式记录的 Cypher 查询示例。
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 - 打开 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.

Neo4j - Skip Clause
SKIP 子句用于定义从哪一行开始包含输出中的行。
The SKIP clause is used to define from which row to start including the rows in the output.
Example
在继续示例之前,请像下面一样创建 5 个节点。
Before proceeding with the example, create 5 nodes 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"})
以下是返回数据库中跳过前 3 个节点的所有节点的 Cypher 查询示例。
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
执行上述查询,执行以下步骤:
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.

Skip Using Expression
你可以使用表达式跳过某个结果的记录。
You can skip the records of a result using an expression.
Example
以下是使用 SKIP 子句和表达式的 Cypher 查询示例。
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 - 打开 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.

Neo4j - With Clause
可以使用 WITH 子句将查询部分链接起来。
You can chain the query arts together using the WITH clause.
Syntax
以下是 WITH 子句的语法。
Following is the syntax of the WITH clause.
MATCH (n)
WITH n
ORDER BY n.property
RETURN collect(n.property)
Example
以下是一个示例 Cypher 查询,演示了 WITH 子句的用法。
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)
执行上述查询,执行以下步骤:
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.

Neo4j - Unwind Clause
unwind 子句用于将列表解压缩成序列行。
The unwind clause is used to unwind a list into a sequence of rows.
Example
以下是解压缩列表的 Cypher 查询示例。
Following is a sample Cypher Query which unwinds a list.
UNWIND [a, b, c, d] AS x
RETURN x
执行上述查询,执行以下步骤:
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.

Neo4j - String Functions
和 SQL 一样,Neo4J CQL 提供了一组字符串函数,可以在 CQL 查询中使用它们来获取所需的结果。
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.
String Functions List
以下是 Neo4J 中著名的字符串函数列表。
Following is the list of prominent String functions in Neo4j.
Sr.No |
Function & Description |
1 |
UPPERIt is used to change all letters into upper case letters. |
2 |
LOWERIt is used to change all letters into lower case letters. |
3 |
SUBSTRINGIt is used to get substring of a given String. |
4 |
*Replace*It is used to replace a substring with a given substring of a String. |
Neo4j - Aggregation Function
与 SQL 类似,Neo4j CQL 在 RETURN 子句中提供了一些聚合函数供使用。它类似于 SQL 中的 GROUP BY 子句。
Like SQL, Neo4j CQL has provided some aggregation functions to use in RETURN clause. It is similar to GROUP BY clause in SQL.
我们可以在 MATCH 命令中使用这些 RETURN + 聚合函数对一组节点执行操作并返回一些聚合值。
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
以下列出 Neo4j 中的聚合函数:
Following is the list of aggregation functions in Neo4j.
Sr.No |
Function & Description |
1 |
COUNTIt returns the number of rows returned by MATCH command. |
2 |
MAXIt returns the maximum value from a set of rows returned by MATCH command. |
3 |
MINIt returns the minimum value from a set of rows returned by MATCH command. |
4 |
SUMIt returns the summation value of all rows returned by MATCH command. |
5 |
AVGIt returns the average value of all rows returned by MATCH command. |
Neo4j - Backup & Restore
在实时应用程序中,我们应该定期备份我们的应用程序数据库,这样我们可以在任何故障时恢复到工作状态。
In real-time applications, we should take backup of our application database regularly, so that we can restore to some working condition at any failure point.
此规则适用于 RDBMS 和 NoSQL 数据库。
This rule is applicable for both RDBMS and NoSQL databases.
在本节中,我们将讨论两项重要的 DBA 任务。
In this section, we are going to discuss about two important DBA tasks.
-
How to back up a Neo4j Database.
-
How to restore Neo4j Database to a specific backup.
Note - 这些步骤仅适用于 Windows 操作系统。我们应该使用类似的命令在其他操作系统中执行相同的步骤。
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.
Neo4j Database Backup
Step 1 - 使用以下路径单击“Neo4j Community” -
Step 1 − Click "Neo4j Community" using the following path −
Windows “开始”按钮 → “所有程序” → “Neo4j Community” → “Neo4j Community”
Windows "Start" button → "All Programs" → "Neo4j Community" → "Neo4j Community"
默认情况下,它会选择 c:\Users\[username]\Documents\Neo4j\default.graphdb. 但如果我们愿意,我们可以将路径更改为不同的目录。
By default, it selects c:\Users\[username]\Documents\Neo4j\default.graphdb. However if we wish to, we can change the path to a different directory.
Step 2 - 在这里我们已经更改为我们的 Neo4j 数据库文件夹。
Step 2 − Here we have changed to our Neo4j Database folder.
C:\Ne04j2.0db

Step 3 - 点击“开始”按钮。
Step 3 − Click the "Start" button.

服务器启动后,我们可以观察我们的 Neo4j 数据库文件是在指定目录中生成的。
Once the server starts, we can observe our Neo4j Database files are generated at a specified directory.

在进行数据库备份之前,我们应该做的第一件也是最重要的事情是关闭 Neo4j 数据库服务器。
Before taking a database backup, the first and foremost thing we should do is shutdown the Neo4j Database server.
Step 4 - 点击“停止”按钮关闭服务器。
Step 4 − Click the "Stop" button to shut down the server.

Neo4j 数据库文件可在 C:\Ne04j2.0db 中找到。
Neo4j Database files are available at C:\Ne04j2.0db

Step 5 - 打开命令提示符。
Step 5 − Open the command prompt.

Step 6 − 在 C:\Neo4j 创建一个“Neo4jDbBackup-01”文件夹(此文件夹可以存在于文件系统中的任何位置)。
Step 6 − Create a folder "Neo4jDbBackup-01" at C:\Neo4j (This may be any location in your file system).
mkdir C:\Neo4j\Neo4jDbBackup-01

它会在指定的“C:\Neo4j\”文件系统位置创建新的“Neo4jDbBackup-01”文件夹。
It creates a new folder "Neo4jDbBackup-01" at the specified file system location "C:\Neo4j\"

Step 7 − 键入以下命令并按 Enter 键。
Step 7 − Type the following command and press Enter key.
copy C:\Ne04j2.0db C:\Neo4j\Neo4jDbBackup-01


这意味着我们的文件已复制到所需的目标文件夹。访问该文件夹,并查看该文件夹是否包含我们的数据库文件。
This means our files are copied to the required destination folder. Access that folder and observe that the folder has our database files.

Step 8 − 使用任何 Windows 压缩/解压工具,例如 WinZip、7 Zip 或 WinRAR 来压缩我们的数据库文件夹。
Step 8 − Use any Windows compression/decompression tool like WinZip, 7 Zip, or WinRAR to zip our Database folder.

Step 9 − 现在已创建我们的 Neo4jDbBackup-01.zip 文件。如果文件系统中存在任何内存限制,请删除“C:\Neo4j\”中的“Neo4jDbBackup-01”文件夹。
Step 9 − Now our Neo4jDbBackup-01.zip file is created. If you have any memory constraints in your file system, then remove "Neo4jDbBackup-01" folder at "C:\Neo4j\"

Neo4j Database Restore
Step 1 − 关闭数据库服务器。请参阅前面的步骤以关闭服务器。
Step 1 − Shutdown the database server. Please refer to the previous steps to shut down the server.

Step 2 − 清空当前数据库文件夹。
Step 2 − Empty the current database folder.

Step 3 − 使用任何 Windows 压缩/解压工具,例如 WinZip、7 Zip 或 WinRar 来解压我们的备份文件夹。
Step 3 − Use any Windows compression/decompression tool like WinZip, 7 Zip, or WinRar to unzip our backup folder.

Step 4 − 打开命令提示符并执行以下命令。
Step 4 − Open the command prompt and execute the following command.
Copy C:\Neo4j\Neo4jDbBackup-01 C:\Ne04j2.0db

现在我们可以看到我们的数据库文件夹包含正常工作的备份文件。
Now we can observe that our database folder contains working backup files
Step 5 − 通过单击“开始”按钮启动服务器。
Step 5 − Start the server by clicking the "Start" button.


Step 6 − 执行一些 MATCH + RETURN 命令以验证是否已正确还原了数据库。
Step 6 − Execute some MATCH + RETURN commands to verify whether we have restored our database properly.
Neo4j - Index
Neo4j SQL 支持节点或关系属性上的索引以提升应用程序性能。我们可以在具有相同标签名称的所有节点上为属性创建索引。
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.
我们可以在 MATCH 或 WHERE 或 IN 运算符上使用这些索引列以提升 CQL 命令的执行。
We can use these indexed columns on MATCH or WHERE or IN operator to improve the execution of CQL command.
在本章中,我们将讨论如何 −
In this chapter, we will discuss how to −
-
Create an Index
-
Delete an Index
Creating an Index
Neo4j CQL 提供了“CREATE INDEX”命令在 Node 或 Relationship 属性上创建索引。
Neo4j CQL provides "CREATE INDEX" command to create indexes on Node or Relationship properties.
Syntax
以下是在 Neo4j 中创建索引的语法。
Following is the syntax to create an index in Neo4j.
CREATE INDEX ON:label (node)
Example
在继续执行示例之前,创建节点 Dhawan,如下所示。
Before proceeding with the example, create a node Dhawan as shown below.
CREATE (Dhawan:player{name: "shikar Dhawan", YOB: 1995, POB: "Delhi"})
以下是 Cypher 查询示例,在 Neo4j 中为节点 Dhawan 创建索引。
Following is a sample Cypher Query to create an index on the node Dhawan in Neo4j.
CREATE INDEX ON:player(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 below.

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.

Deleting an Index
Neo4j CQL 提供了“DROP INDEX”命令来删除 Node 或 Relationshis 属性的现有索引。
Neo4j CQL provides a "DROP INDEX" command to drop an existing index of a Node or Relationshis property.
Syntax
以下是在 Neo4j 中创建索引的语法。
Following is the syntax to create an index in Neo4j.
DROP INDEX ON:label(node)
Example
以下是 Cypher 查询示例,在 Neo4j 中为名为“Dhawan”的节点创建索引。
Following is a sample Cypher Query to create an index on the node named “Dhawan” in Neo4j.
DROP INDEX ON:player(Dhawan)
执行上述查询,执行以下步骤:
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.

Neo4j - Create Unique Constraint
在 Neo4j 数据库中,CQL CREATE 命令始终会创建新节点或关系,这意味着即使您使用相同的值,它也会插入新行。根据我们的应用程序要求的一些节点或关系,我们必须避免这种重复。为此,我们应使用一些数据库约束在节点或关系的一个或多个属性上创建规则。
In Neo4j database, CQL CREATE command always creates a new node or relationship which means even though you use the same values, it inserts a new row. As per our application requirements for some nodes or relationships, we have to avoid this duplication. For this, we should use some database constraints to create a rule on one or more properties of a node or relationship.
与 SQL 类似,Neo4j 数据库还支持节点或关系属性上的 UNIQUE(唯一)约束。UNIQUE 约束用于避免重复记录并强制执行数据完整性规则。
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.
Create UNIQUE Constraint
Neo4j CQL 提供“CREATE CONSTRAINT”命令,用于针对节点或关系属性创建唯一约束。
Neo4j CQL provides "CREATE CONSTRAINT" command to create unique constraints on node or relationship properties.
Syntax
以下是 Neo4j 中创建 UNIQUE 约束的语法。
Following is the syntax to create a UNIQUE constraint in Neo4j.
MATCH (root {name: "Dhawan"})
CREATE UNIQUE (root)-[:LOVES]-(someone)
RETURN someone
Example
在继续示例之前,如以下所示创建 4 个节点。
Before proceeding with the example, create 4 nodes as shown below.
CREATE(Dhawan:player{id:001, name: "shikar Dhawan", YOB: 1995, POB: "Delhi"})
CREATE(Jonathan:player {id:002, name: "Jonathan Trott", YOB: 1981, POB: "CapeTown"})
CREATE(Sangakkara:player {id:003, name: "Kumar Sangakkara", YOB: 1977, POB: "Matale"})
CREATE(Rohit:player {id:004, name: "Rohit Sharma", YOB: 1987, POB: "Nagpur"})
CREATE(Virat:player {id:005, name: "Virat Kohli", YOB: 1988, POB: "Delhi"})
以下是使用 Neo4j 根据 id 属性创建 UNIQUE 约束的 Cypher 查询示例。
Following is a sample Cypher Query to create a UNIQUE constraint on the property id using Neo4j.
CREATE CONSTRAINT ON (n:player) ASSERT n.id IS UNIQUE
执行上述查询,执行以下步骤:
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.

Verification
现在,尝试添加具有冗余 id 值的另一个节点。在这里,我们尝试创建一个 id 为 002 的节点。
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: "NavagamGhed"})
如果您执行此查询,您将获得以下屏幕截图中显示的错误消息。
If you execute this query, you will get an error message as shown in the following screenshot.

Neo4j - Drop Unique
我们在前一章中已经讨论了使用示例创建 UNIQUE 约束操作。在本章中,我们将讨论删除 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.
Neo4j CQL 提供“DROP CONSTRAINT”命令,用于从节点或关系属性中删除现有的 Unique 约束。
Neo4j CQL provides "DROP CONSTRAINT" command to delete existing Unique constraint from a node or relationship property.
Syntax
以下是 Neo4j 中删除 UNIQUE 约束的语法。
Following is the syntax for dropping a UNIQUE constraint in Neo4j.
DROP CONSTRAINT ON (node:label)
ASSERT node.id IS UNIQUE
Example
以下是删除属性 id 上的 UNIQUE 约束的 Cypher 查询示例。
Following is a sample Cypher Query to remove the UNIQUE constraint on the property id.
DROP CONSTRAINT ON (n:player)
ASSERT n.id IS UNIQUE
执行上述查询,执行以下步骤:
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.
