Search code examples
neo4jcypherany

ANY function returns nothing when the database is empty


I have a cypher query which is looking to see if an account with a certain email address already exists in the database. To do this I use the built in ANY function:

MATCH (a:Account)
RETURN ANY(x IN a.email WHERE x='[email protected]') AS exists

since this is a development database the test records are sometimes deleted when a significant change to the data structure is made resulting in the database being empty. The problem I'm having is that the query shown above returns neither true or false instead it just returns nothing when the database is empty. This is problematic since this query is called by a php script using the graph-aware library and when I run the getRecord() it returns an error because the function returned nothing.

Is there a way to make the ANY function return false if the database is empty.


Solution

  • 1) The cypher-query can be simplified:

    MATCH (a:Account) WHERE '[email protected]' IN a.email
    RETURN COUNT(a) > 0 AS exists
    

    2) Use the driver features:

    $result = $client->run('
      MATCH (a:Account) WHERE \'[email protected]\' IN a.email
      RETURN COUNT(a) > 0 AS exists
    ')
    
    var_dump($result->size() && $result->getRecord())
    
    // or      .
    
    var_dump($result->firstRecordOrDefault(false))