我正在寻找一个单一的 Cypher查询,该查询将增加一个整数节点参数并返回该值,同时将其初始化到0,以防它不存在。
类似于下面的伪Cypher:
MATCH (n:Order)
IF n.product_count IS NULL THEN n.product_count = 0 // this line is what I need
SET n.product_count = n.product_count + 1
RETURN n.product_count我能够将一个查询与完成任务的FOREACH语句组合在一起,但这似乎很麻烦,而且不适合我的用例:
MATCH (n:Order)
WHERE id(n) = 9503
FOREACH ( i in (CASE WHEN n.product_count IS NULL THEN [1] ELSE [] END) | SET n.product_count = 0 )
SET n.product_count = n.product_count + 1
RETURN n.product_count; 怎么做才是正确的呢?
注意:Order节点非常复杂,包含许多其他属性,因此在这种情况下,MERGE状态是非常不需要的。
发布于 2014-12-27 14:07:33
Neo4j为这种情况提供了一个有用的函数,称为coalesce。
合并使用任意数量的参数,并返回非空的第一个参数。在所有参数都为空的情况下,它只返回NULL。
因此,例如:
coalesce(NULL, 1) // equates to 1
coalesce("hello", 6) // equates to "hello"
coalesce(NULL, "world", NULL) // equates to "world"
coalesce(NULL, NULL) // equates to NULL因此,您的查询如下所示:
MATCH (n:Order)
SET n.product_count = coalesce(n.product_count, 0) + 1
RETURN n.product_count以下是关于合并的正式文档:
http://neo4j.com/docs/stable/query-functions-scalar.html#functions-coalesce
https://stackoverflow.com/questions/27666841
复制相似问题