lisaBZT
lisaBZT

Reputation: 93

"No relation" in cypher query

How is it possible in cypher to have a query like: "return all people that anna follows that don't follow anyone"?

In the following (where I have the id of the start node made clear after the query) the "r is null"-part does not seem to work:

START o=node({id}) 
MATCH (a)-[:follows]->(b)-[r]->(c) 
WHERE a.name="anna" and r is null 
RETURN b

Right now, "follows" is the only relation I have. But also

START o=node({id}) 
MATCH (a)-[:follows]->(b)-[:follows]->(c)
WHERE a.name="anna" and c is null
RETURN b* does not work.

By doesn't work I mean: I don't get any results although there should be some.

Upvotes: 8

Views: 3759

Answers (1)

Eve Freeman
Eve Freeman

Reputation: 33145

It won't match a pattern if it doesn't exist. match is for finding things, not for not finding things. You can put a predicate like that into the where clause:

START a=node({id}) 
MATCH (a)-[:follows]->(b)
WHERE not(b-[:follows]->()) 
RETURN b

Upvotes: 18

Related Questions