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...
Return all elements When you want to return all nodes, relationships and paths found in a query, you can use the * symbol.
The count store also keeps count of the relationships with respect to the label of a single end-node.
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.
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.
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.
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
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);
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With