Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
01-21-2021 07:49 AM
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
Solved! Go to Solution.
01-21-2021 11:27 AM
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
01-21-2021 08:47 AM
nodes with no relationships is
match (n) where size ( (n)-[r]-() ) = 0 return n;
nodes with no properties
01-21-2021 11:09 AM
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
01-21-2021 11:27 AM
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
All the sessions of the conference are now available online