Search code examples
neo4jcypher

Neo4J Cypher data type conversion


I've got a property quantity on our Product-nodes and am looking to do a cypher query that gives me all nodes with quantity = 20 ... problem is that quantity is stored as a string in neo4j. Is there a way to cast the property to integer in the cypher query?

// This fails to find the required nodes
MATCH (p:Product) WHERE p.quantity = 20;

// This finds them
MATCH (p:Product) WHERE p.quantity = "20";

// I would like to do this
MATCH (p:Product) WHERE INT(p.quantity) = 20;

PS: this is a really simplified usecase, we don't really have products and quantities but are just faced with existing neo4j data that has integer values stored as strings, and we would like to do some matches on these strings


Solution

  • You can do it the other way round.

    MATCH (p:Product) WHERE p.quantity = str(20) RETURN p;
    

    should also work with params.

    MATCH (p:Product) WHERE p.quantity = str({quantity}) RETURN p;
    

    or even with inline property matches

    MATCH (p:Product {quantity : str({quantity})}) RETURN p;