Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find nodes without particular relation

Tags:

neo4j

cypher

Let's assume the following situation. We have database with Person nodes and Knows relations. Finding people that know each other is trivial, but how to write the query that returns all people that particular person does not know? The only way I could think of it was:

match (n), (m) where not (n)-->(m) and n.id = 1 return n,m;

The obvious downside of this query is cartesian product, so with large amount of data this query would be terribly slow. But is there any other solution possible? I tried with OPTIONAL MATCH, but it did not work out as there is no m node when r is null.

like image 482
Fuv Avatar asked Jun 26 '26 00:06

Fuv


1 Answers

It's technically a cartesian product, but since once side in anchored to an individual node really the search space is just on the m. Have you tried benchmarking the query?

Also, I wonder about trying this:

MATCH (m) WHERE NOT (n {id: 1})-->(m)
RETURN n,m;
like image 165
Brian Underwood Avatar answered Jun 28 '26 07:06

Brian Underwood