Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"No relation" in cypher query

Tags:

neo4j

cypher

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.

like image 884
lisaBZT Avatar asked Apr 07 '13 22:04

lisaBZT


1 Answers

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
like image 80
Eve Freeman Avatar answered Oct 01 '22 11:10

Eve Freeman