cancel
Showing results for 
Search instead for 
Did you mean: 

Nodes without any properties

janezic
Graph Voyager

Hello,

I habe found how to query for nodes without labels, but how do I have to write a query for all the nodes in the database that do not have any property and/or any relationship?

The background of my question: I have imported a CSV but due to misspelling in the Cypher-statement, I have created 20.000 „empty“ nodes without a label and without properties. I know how to find the ones without label, but Before deleting them I want to make sure that these are also the ones without properties and relationships.

Thank you,

JJJ

1 ACCEPTED SOLUTION

janezic
Graph Voyager

Thank you both, basically these are the solutions, but slightly different:

This is for the nodes without relationships:

MATCH (n) WHERE size (  (n)-[]-() ) = 0 RETURN n;

So, it’s square brackets for the relationship and no r within.

And this is for the nodes without labels:

MATCH (n) WHERE size(labels(n)) = 0 RETURN n

And this is for nodes without labels and without properties:

MATCH (n) WHERE size(labels(n)) = 0 AND properties(n) = {} RETURN n

THX again for your quick answers,

JJJ

View solution in original post

3 REPLIES 3

dana_canzano
Neo4j
Neo4j

nodes with no relationships is

match (n) where size (  (n)-[r]-() ) = 0 return n;

nodes with no properties

tard_gabriel
Ninja
Ninja

TESTED ON Neo4j 4.2.2

MATCH (n) WHERE size((n--())) = 0 AND properties(n) = {} AND labels(n) =
RETURN n

Explanation:
As the function properties will always return the properties map of a node or relation, you will get an empty map for those who don't have any properties. Same for labels except that the return object is an empty list.

WARNING:
This query is not looking for the nodes without labels:

MATCH (n)
RETURN n

janezic
Graph Voyager

Thank you both, basically these are the solutions, but slightly different:

This is for the nodes without relationships:

MATCH (n) WHERE size (  (n)-[]-() ) = 0 RETURN n;

So, it’s square brackets for the relationship and no r within.

And this is for the nodes without labels:

MATCH (n) WHERE size(labels(n)) = 0 RETURN n

And this is for nodes without labels and without properties:

MATCH (n) WHERE size(labels(n)) = 0 AND properties(n) = {} RETURN n

THX again for your quick answers,

JJJ