Neo4J Cypher data type conversion
Asked Answered
M

4

14

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

Medalist answered 15/1, 2014 at 8:54 Comment(0)
C
12

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;
Cosmography answered 15/1, 2014 at 9:56 Comment(3)
Still no way to convert to integer?Candytuft
Ah, here it is: docs.neo4j.org/chunked/stable/…Candytuft
Looks like str may have been renamed to toString, see the relevant current (3.x) documentation.Verdha
C
9
MATCH (p:Product) WHERE toInt(p.quantity) = 20;
Cienfuegos answered 24/2, 2015 at 5:39 Comment(3)
It's always a good idea to put some text in your answer to explain what you're doing. Read how to write a good answer.Northwestward
toInt is the needed function for meSldney
just as information: toInt is deprecated, please use toInteger()Scholasticism
C
0

I too have been faced with that problem earlier. As far as I found out, it was not possible to do that conversion directly in cypher. I used a small Java script (using the standard Java API) to change the data types of the stored values. This is a couple of months ago though, so it might have changed with the 2.0 version.

Commodus answered 15/1, 2014 at 9:12 Comment(1)
Yes, i've seen the google groups discussion but was hoping something had changed in neo4j 2.0 ... i haven't found anything in the documentation tho.Medalist
I
0

toInteger worked for me.

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

I am on neo4j version Version: 5.6.0

Imperious answered 16/5, 2023 at 7:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.