Neo4j 如何在使用带有空列表的head函数时获取空值
我有一个类似这样的密码查询Neo4j 如何在使用带有空列表的head函数时获取空值,neo4j,cypher,Neo4j,Cypher,我有一个类似这样的密码查询 START dep=node:cities(city_code = "JGS"), arr=node:cities(city_code = "XMN") MATCH dep-[way:BRANCH2BRANCH_AIRWAY*0..1]->()-->arr RETURN length(way), transfer.city_code, extract(w in way: w.min_consume_time) AS consumeTime 名为“
START dep=node:cities(city_code = "JGS"),
arr=node:cities(city_code = "XMN")
MATCH dep-[way:BRANCH2BRANCH_AIRWAY*0..1]->()-->arr
RETURN length(way), transfer.city_code,
extract(w in way: w.min_consume_time) AS consumeTime
名为“way”的关系是可选的,因此当关系“way”不存在时,名为“consumeTime”的属性将是空列表
查询结果为:
|0 |“JGS”|[]||1 |“SZX”|[3600]| 当我想使用带有属性“consumeTime”的head函数时,它返回一个错误“Invalid query:head of empty list” 我怎样才能得到这样的结果 |0 |“JGS”| null |
|1 |“SZX”| 3600 |这对于条件表达式来说是微不足道的,我认为这是添加到Cypher中的重要内容: 这里有一个使用
reduce
的有点粗糙的查询,需要1.9-SNAPSHOT:
START dep=node:cities(city_code = "JGS"),
arr=node:cities(city_code = "XMN")
MATCH dep-[way:BRANCH2BRANCH_AIRWAY*0..1]->()-->arr
WITH length(way) as wayLength,
transfer.city_code as transferCityCode,
extract(w in way: w.min_consume_time) AS consumeTime
WITH wayLength,
transferCityCode,
consumeTime,
// reverse the consumeTime list, so that we can get the head from the end
reduce(acc=[], x in consumeTime: x + acc) reverseConsumeTime
RETURN wayLength,
transferCityCode,
consumeTime,
// return null if empty, else the end of the list
reduce(acc=null, x in reverseConsumeTime: x) as headOrNull;
可以使用以下语法对该查询进行大量改进,以反转集合:
reverse(coll)
或条件表达式来检查空列表。更新:在这里发布了一个功能请求(对于您的特定情况,它甚至比条件表达式更好):非常感谢您的回答和功能请求。我也提交了一个拉取请求:拉取请求被合并到master中。应该很快出现在1.9-SNAPSHOT中。