Neo4J Cypher data type conversion

2019-03-15 09:43发布

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

标签: neo4j cypher
3条回答
太酷不给撩
2楼-- · 2019-03-15 09:56

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;
查看更多
放荡不羁爱自由
3楼-- · 2019-03-15 09:58

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.

查看更多
▲ chillily
4楼-- · 2019-03-15 10:13
MATCH (p:Product) WHERE toInt(p.quantity) = 20;
查看更多
登录 后发表回答