Neo4j: eliminate nodes that exists in a path

There is no doubt Neo4j is amazing. It’s our go-to database for web application development because of it’s robustness easy setup. Complex relationships that were once a nightmare to manage in MySQL or Postgres are a breeze in Neo4j.

I was writing a query where I needed all the nodes of a certain type, but not if those nodes were attached across multiple hops to another node.

The relationships looked like this:

[:tickler]-[:tickles]->(:step)-[:comments_on:*0..]->(:case_type|:case)

If the tickler was part of a case, I wanted it. But it if was part of a case_type, I did not want it.

The query ended up looking like this:

MATCH (t:tickler) WHERE t.completed = false AND NOT EXISTS((t)-[*0..]->(:case_type)) RETURN t ORDER BY t.created_at ASC

You can see that I wanted all the ticklers that were completed and they could not be attached across nodes to any case_type.

Neo4j making it easy once again

Leave a Comment

Sign in to post your comment or sign-up if you don't have any account.