简体   繁体   English

查找具有2个关系的节点

[英]Finding nodes with 2 relationships

Not sure if the title explains everything but I'm learning Cypher using Neo4j and the Movie Database (from here: https://neo4j.com/developer/example-data/ ) and I'm trying to craft a query that will give me every movie whose director also acted in it. 不知道标题是否能解释所有内容,但我正在使用Neo4j和电影数据库(来自此处: https//neo4j.com/developer/example-data/ )学习Cypher,并且正在尝试构建一个查询,我每部导演也出演过的电影。

To give an example of such a movie, Pulp Fiction - Quentin Tarantino both acted in and directed the movie. 例如,《低俗小说》-昆汀·塔伦蒂诺(Quentin Tarantino)扮演并导演了这部电影。

I came up with this query for the above example: 我针对以上示例提出了此查询:

match (m:Movie)-[:ACTS_IN]-(d:Director)-[:DIRECTED]->(n:Movie)
where d.name STARTS WITH 'Q'
return *

But it doesn't seem to work the way I expected it to. 但这似乎并没有达到我的预期。 I get all the movies he's directed and acted in, whereas I only want the ones where he's done both. 我得到了他执导和表演的所有电影,而我只希望他既完成了这两个方面的电影。

You already had d and m declared, so you don't assign labels in the second MATCH statement again. 您已经声明了dm ,因此不必在第二个MATCH语句中再次分配标签。 The correct query would look like this 正确的查询如下所示

MATCH (d:Director)-[:DIRECTED]->(m:Movie) 
WHERE d.name STARTS WITH 'Q'     
MATCH (d)-[:ACTS_IN]->(m) 
RETURN d, collect(m) AS movies

Note that based on your initial question, I've modified the return statement as well. 请注意,根据您的最初问题,我还修改了return语句。

Oh wow I just figured it out: 哦,我刚刚想出了:

match (d:Director)-[:DIRECTED]->(m:Movie)
match (d:Director)-[:ACTS_IN]->(m:Movie)
where d.name STARTS WITH 'Q' 
return *

I was honestly expecting to have to use some kind of operator to join the two, but I guess I was wrong. 老实说,我原本希望使用某种运算符来加入两者,但我想我错了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM