0Pricing
Neo4j Graph Database Fundamentals · 课时

使用 Cypher MATCH 匹配模式

掌握 MATCH 子句,在图数据中查找特定的节点和关系模式

使用 Cypher MATCH 匹配模式 是 CoddyKit 上的免费 Neo4j Graph Database Fundamentals 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Neo4j Graph Database Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Neo4j Graph Database Fundamentals 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Discovering Data with MATCH

Welcome to the core of graph querying! The MATCH clause in Cypher is how you find specific patterns of data in your Neo4j graph.

Think of it like drawing the shape of the data you want to find. You describe nodes, relationships, and their properties, and Neo4j finds all instances that fit your description.

Finding Any Node

The simplest MATCH query finds any node in your graph. You represent a node using parentheses ().

Adding a variable inside, like (n), lets you refer to that node later, for example, to return its properties. We'll use LIMIT to show just a few results.

MATCH (n)
RETURN n LIMIT 3

Matching Nodes by Label

Graphs often have different types of nodes. In Cypher, these types are called labels. You can specify a label to find only nodes of a particular type.

Labels are placed after the node variable, separated by a colon: (variable:Label).

MATCH (p:Person)
RETURN p.name LIMIT 3

Matching Nodes by Property

Nodes also have properties, which are key-value pairs describing them. You can filter nodes based on these properties directly within your MATCH clause.

Properties are placed inside curly braces {} after the label (or variable if no label): (node:Label {key: 'value'}).

MATCH (m:Movie {title: 'The Matrix'})
RETURN m.releaseYear

Introducing Relationships

Relationships define how nodes are connected. In MATCH, you represent a relationship using dashes and angle brackets: -- for undirected, --> for directed, and <-- for reverse directed.

You can also assign a variable to the relationship, like -[r]-, to work with it.

MATCH (a)-[r]->(b)
RETURN a.name, TYPE(r), b.title LIMIT 3

Matching Relationships by Type

Just like nodes have labels, relationships have types. Specifying a relationship type helps you find specific kinds of connections between nodes.

The type is placed after the relationship variable, separated by a colon: -[r:TYPE]->.

MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
RETURN p.name, m.title LIMIT 3

Matching with Direction

Relationships are directional! The arrows in your MATCH pattern indicate the flow. If you omit arrows (e.g., (a)-[r]-(b)), the match will be undirected, finding connections regardless of their stored direction.

It's generally best practice to specify direction when you know it.

MATCH (director:Person)<-[:DIRECTED]-(movie:Movie)
RETURN director.name, movie.title LIMIT 3

Combining Node & Relationship Patterns

The real power of MATCH comes from combining nodes and relationships to describe complex patterns in your graph. You can chain multiple nodes and relationships together.

This allows you to find paths, connections through intermediate nodes, and more.

MATCH (p:Person {name: 'Tom Hanks'})-[:ACTED_IN]->(m:Movie)<-[:DIRECTED]-(d:Person)
RETURN p.name, m.title, d.name

Finding Optional Connections

Sometimes, a part of your pattern might not always exist. OPTIONAL MATCH tries to find the pattern, but if it doesn't, the unmatched parts will be null instead of failing the whole query.

This is useful when you want to see existing connections while still returning the main node even if a specific connection is missing.

MATCH (p:Person {name: 'Keanu Reeves'})
OPTIONAL MATCH (p)-[r]->(m:Movie)
RETURN p.name, TYPE(r), m.title

Match Clause Challenge

Which Cypher MATCH clause correctly finds all movies directed by 'Quentin Tarantino'?

Recap: Mastering MATCH

Great job! You've learned the fundamental skill of finding patterns in your graph using the MATCH clause.

  • Use () for nodes, -- for relationships.
  • Specify labels (:Label) for node types and types (:TYPE) for relationships.
  • Filter by properties using {}.
  • Arrows (-->, <--) define relationship direction.
  • OPTIONAL MATCH helps find patterns that might not always exist without breaking the query.

Next, we'll explore how to refine your matched data using WHERE and ORDER BY!

常见问题解答

「使用 Cypher MATCH 匹配模式」课时是免费的吗?

是的 — 「使用 Cypher MATCH 匹配模式」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Neo4j Graph Database Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Neo4j Graph Database Fundamentals 课程共包含 4 节课。

「使用 Cypher MATCH 匹配模式」这节课中我会学到什么?

掌握 MATCH 子句,在图数据中查找特定的节点和关系模式 你通过在浏览器中直接运行的动手代码来练习 Neo4j Graph Database Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Neo4j Graph Database Fundamentals 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Neo4j Graph Database Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「使用 Cypher MATCH 匹配模式」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Neo4j Graph Database Fundamentals 课中编写并运行代码吗?

能。每节 Neo4j Graph Database Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 节点、关系与属性
  2. 使用 Cypher CREATE 创建数据
  3. 使用 Cypher MATCH 匹配模式
  4. 使用 RETURN 返回并塑造结果
← 返回 Neo4j Graph Database Fundamentals