cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Conditions in Repository query

myshareit
Node Clone

I have the following Spring Data repository method:

    @Query(value = "MATCH (p:Person) WHERE p.name = $name and p.age = $age RETURN p", countQuery = "MATCH (p:Person) WHERE p.name = $name and p.age = $age RETURN count(p)")
    Page<Person> findAllPersons(String name, Integer age, Pageable pageable);

sometimes I'll pass NULL as a name or age parameters (or both of them) and in such case don't want that null parameters were evaluated in WHERE Cypher statement. Is it possible to manage such case with pure Cypher in order to check name or/and age on NULL and exclude them from WHERE ? Thanks

1 ACCEPTED SOLUTION

anthapu
Graph Fellow

You can try

MATCH (p:Person) 
WHERE 
    ( $name is null OR p.name = $name ) 
    AND 
    ( $age is null OR p.age = $age ) RETURN p

View solution in original post

1 REPLY 1

anthapu
Graph Fellow

You can try

MATCH (p:Person) 
WHERE 
    ( $name is null OR p.name = $name ) 
    AND 
    ( $age is null OR p.age = $age ) RETURN p