node.jsneo4jcypher

Neo4J always defaults NodeJS numbers as float when using parametrized queries


I'm trying to use the Neo4j with NodeJS, using parameters to save some values into a Neo4j node, something like this:

{
    query: 'MATCH (n{id:$id}) SET n.n1 = $number RETURN n',    
    params: { id: 'someId', number: 15 } 
}

When I view the data in Neo4j, the data always appears as {n1: 15.0} rather than {n1: 15}. Is there an easy way to keep the numbers as integers unless there's an explicit decimal in the number?


Solution

  • You can use the TOINTEGER() function to convert to an integer:

    MATCH (n{id:$id}) SET n.n1 = TOINTEGER($number) RETURN n
    

    [UPDATED]

    If you are using the official neo4j Javascript driver, then you should read up on its special support for integers in Javascript (to work around the fact that Javascript does not support 64-bit integers, as used by neo4j).

    In your case, you should use the driver's neo4j.int() method to pass an integer parameter value, otherwise it will be passed as a float.

    For example:

    var neo4j = require('neo4j-driver').v1;
    
    var result = session.run(
      "MATCH (n{id:$id}) SET n.n1 = $number RETURN n",
      {id: 'abc123', number: neo4j.int(15)}
    );