Maciej Ziarko
Maciej Ziarko

Reputation: 12084

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

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?

Upvotes: 2

Views: 147

Answers (2)

Maciej Ziarko
Maciej Ziarko

Reputation: 12084

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

Upvotes: 0

Stefan Armbruster
Stefan Armbruster

Reputation: 39915

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

Upvotes: 4

Related Questions