5
votes

I would like to check whether a node exists using its name (instead of its ID). The Cypher query looks like :

MATCH (c:Jaguar{name:"JLR 2.5Ltr"})-[:REPRESENTED_BY]->(v) RETURN c IS NOT NULL

However, Using neo4j shell/web console, the result returned is of type String. The same fails in spring-data-neo4j with error :

Null return value from advice does not match primitive return type for: public abstract boolean xxx.yyy.repository.SomeRepository.checkIfDatasetExists(java.lang.String)

Has somebody come across any work around for this

2

2 Answers

7
votes

The answer provided by Supamiu will not work unfortunately, you need to hack this by returning a count expression :

MATCH (c:Jaguar{name:"JLR 2.5Ltr"})-[:REPRESENTED_BY]->(v) 
RETURN count(c) > 0 as c
2
votes

You should use CASE to check if your node is null or not, and return the value you need :

MATCH (c:Jaguar{name:"JLR 2.5Ltr"})-[:REPRESENTED_BY]->(v)
RETURN CASE WHEN c IS NULL THEN false ELSE true END as c

More informations can be found on Neo4j's Documentation