cancel
Showing results for 
Search instead for 
Did you mean: 

Join the community at Nodes 2022, our free virtual event on November 16 - 17.

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