Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

neo4j return an integer instead of {high: 0, low: 10}

Tags:

neo4j

cypher

Is there a way to return just an integer through cypher?

I am working with neo4j and the javascript driver. However when I do a count() I get {low: 10, high: 0}. I would like to force this to return a low integer instead of the object above. Is this possible through cypher?

Note that I don't want to do this in the neo4j javascript driver and prefer to do this in cypher since I know it will be a low number...

like image 994
Terence Chow Avatar asked Mar 07 '17 10:03

Terence Chow


People also ask

How do I return all nodes and relationships in Neo4j?

Return all elements When you want to return all nodes, relationships and paths found in a query, you can use the * symbol.

Which of the given below query is used to return the count of the relationship for a node?

The count store also keeps count of the relationships with respect to the label of a single end-node.

How do you perform an aggregation in Cypher?

The Cypher query offers aggregation similar to GROUP BY offered by SQL. The aggregate function can take multiple values and can calculate the aggregated values for them. In this recipe, we will learn the common aggregation techniques, with the help of examples.

How do you count nodes in Cypher?

Using count(*) to return the number of nodes The function count(*) can be used to return the number of nodes; for example, the number of nodes connected to some node n . The labels and age property of the start node n and the number of nodes related to n are returned.


4 Answers

Cypher only works with Long and Double values. Javascript isn't able to adequately represent 64-bit Longs.

There's advice here from the Neo4j javascript driver for handling this when providing parameters to Neo4j queries, as well as for handling returned longs.

like image 81
InverseFalcon Avatar answered Oct 09 '22 20:10

InverseFalcon


Starting from 1.6 version of the driver it is possible to configure it to only return native numbers instead of custom Integer objects. The configuration option affects all integers returned by the driver. Enabling this option can result in a loss of precision and incorrect numeric values being returned if the database contains integer numbers outside of the range [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER].

var driver = neo4j.driver(
  'neo4j://localhost',
  neo4j.auth.basic('neo4j', 'neo4j'),
  { disableLosslessIntegers: true }
)

Source: https://github.com/neo4j/neo4j-javascript-driver#a-note-on-numbers-and-the-integer-type

Keep in mind that MAX_SAFE_INTEGER is 2^53-1 = 9,007,199,254,740,991

like image 27
pawansgi92 Avatar answered Oct 09 '22 21:10

pawansgi92


You can write your simple handler for the transformation:

var transformIntegers = function(result) {
  return new Promise( (resolve,reject) => {
    try {
      result.records.forEach( function(row, i) {
        row._fields.forEach( function(val, j) {
          result.records[i]._fields[j] = neo4j.isInt(val) 
              ? (neo4j.integer.inSafeRange(val) ? val.toNumber() : val.toString()) 
              : val;
        })
      })
      resolve(result);
    } catch (error) {
        reject( error );
    }
  });
};

And as an example:

session
  .run('MATCH (A) RETURN ID(A) AS id, toInteger(10^20) as unsafe LIMIT 1')
  .then( transformIntegers )
  .then( function(result) {
    console.log(result.records[0]);
    session.close();
    driver.close();
  })
  .catch( function(error) {
    console.log(error);
  });
like image 42
stdob-- Avatar answered Oct 09 '22 21:10

stdob--


Use neo4j.integer.toNumber. So basically when you want to represent such value, use something like

variable.toNumber()

You will get a good old int value.

like image 25
EliuX Avatar answered Oct 09 '22 21:10

EliuX