0Pricing
Neo4j Graph Database Fundamentals · Lekcja

Dopasowywanie wzorców za pomocą Cypher MATCH

Opanuj klauzulę MATCH, aby wyszukiwać określone wzorce węzłów i relacji w danych grafowych.

Dopasowywanie wzorców za pomocą Cypher MATCH to bezpłatna lekcja Neo4j Graph Database Fundamentals na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Neo4j Graph Database Fundamentals, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Neo4j Graph Database Fundamentals zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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!

Często zadawane pytania

Czy lekcja „Dopasowywanie wzorców za pomocą Cypher MATCH” jest bezpłatna?

Tak — pełny tekst „Dopasowywanie wzorców za pomocą Cypher MATCH” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Neo4j Graph Database Fundamentals, przejdź na CoddyKit PRO. Kurs Neo4j Graph Database Fundamentals zawiera 4 lekcji w sumie.

Co nauczysz się w „Dopasowywanie wzorców za pomocą Cypher MATCH”?

Opanuj klauzulę MATCH, aby wyszukiwać określone wzorce węzłów i relacji w danych grafowych. Ćwiczysz Neo4j Graph Database Fundamentals z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Neo4j Graph Database Fundamentals?

Nie wymagamy żadnego doświadczenia. Neo4j Graph Database Fundamentals w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.

Ile czasu zajmuje lekcja „Dopasowywanie wzorców za pomocą Cypher MATCH”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Neo4j Graph Database Fundamentals?

Tak. Każda lekcja Neo4j Graph Database Fundamentals zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Węzły, relacje i właściwości
  2. Tworzenie danych za pomocą Cypher CREATE
  3. Dopasowywanie wzorców za pomocą Cypher MATCH
  4. Zwracanie i kształtowanie wyników za pomocą RETURN
← Powrót do Neo4j Graph Database Fundamentals