Conditions in Repository query

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

You can try

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