Cypher: Finding movies that haven't been rated yet by particular user

。_饼干妹妹 提交于 2020-01-15 03:56:07

问题


Let's say I have 3 movies in my Neo4J database:

CREATE (interpreter:Movie {title: 'The Interpreter', year : 2005})
CREATE (dogville:Movie {title: 'Dogville', year : 2003})
CREATE (railwayMan:Movie {title: 'The Railway Man', year : 2013})

Also there are users:

CREATE (maciej:Person {name: 'Maciej Ziarko', birthYear: 1989})

who rate movies:

CREATE (maciej)-[:RATED {stars : 4, comment : "I liked that movie!" }]->(interpreter);

It's easy to find movies rated by particular user using Cypher

MATCH (person:Person)-[:RATED]->(movie:Movie)
WHERE person.name = 'Maciej Ziarko'
RETURN movie.title

Result:

+-------------------+
| movie.title       |
+-------------------+
| "The Interpreter" |
+-------------------+
1 row

But how can I find movies NOT yet rated by particular user?


回答1:


You might use something like:

MATCH (m:Movie) WHERE NOT (m)<-[:RATED]-() return m.title

In WHERE you can filter for non existing paths using NOT, see http://docs.neo4j.org/chunked/milestone/query-where.html#where-filter-on-patterns-using-not




回答2:


What I did after reading Stefan's answer:

MATCH (movie:Movie), (person:Person)
WHERE person.name = 'Maciej Ziarko' 
     AND (NOT (movie:Movie)<-[:RATED]-(person:Person))
RETURN movie.title

Result:

+-------------------+
| movie.title       |
+-------------------+
| "Dogville"        |
| "The Railway Man" |
+-------------------+
2 rows


来源:https://stackoverflow.com/questions/19543957/cypher-finding-movies-that-havent-been-rated-yet-by-particular-user

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!