https://github.com/famousdraw

Cypher Fundamentals-Finding Relationships

QUIZ

Finding Relationships

VideoTranscript
 

Finding relationships

OUR GOAL

As a movie fanatic
I would like to find movies for a particular actor
So that I can watch a movie this evening

In the previous lesson, we used the MATCH clause to find the node in our database that represented Tom Hanks.

cypher
 
MATCH (p:Person {name: 'Tom Hanks'})
RETURN p

We can extend the pattern in the MATCH clause to traverse through all relationships with a type of ACTED_IN to any node. Our domain model shows that the ACTED_IN relationship goes in an outgoing direction from the Person node so we can add the direction in our pattern. We often refer to this as a traversal.

cypher
Incomplete code
 
MATCH (p:Person {name: 'Tom Hanks'})-[:ACTED_IN]->()

Our data model dictates that the node at the other end of that relationship will be Movie node, so we don’t necessarily need to specify the :Movie label in the node - instead we will use the variable m.

cypher
 
MATCH (p:Person {name: 'Tom Hanks'})-[:ACTED_IN]->(m)
RETURN m.title

This code returns the titles of all moves that Tom Hanks acted in.

If our graph had different labels, for example Television and Movie nodes this query would have returned all Television and Movie nodes that Tom Hanks acted in. That is, if we had multiple types of nodes at the end of the ACTED_IN relationships in our graph, we could make sure that we only return movies.

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

Because our graph only has Movie nodes that have incoming ACTED_IN relationships, this query returns the exact same results as the previous query.

Check your understanding

1. Specifying the relationship direction

Say we wrote our query above differently and started with the Movie node. Use the dropdown below to select the correct direction for the query.

Once you have selected your option, click the Check Results query button to continue.

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

2. Query using a pattern

Which MATCH clauses will return the names of the directors of the movie, The Matrix?

  • MATCH (m:Movie {title: 'The Matrix'})<-[:DIRECTED]-(p:Person) RETURN p.name

  • MATCH (m:Movie {title: 'The Matrix'})--(p:Person) RETURN p.name

  • MATCH (m:Movie {title: 'The Matrix'})<-[:DIRECTED]-(p) RETURN p.name

  • MATCH (m:Movie {title: 'The Matrix'})--(p:Director) RETURN p.name

posted on   红色MINI  阅读(39)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示