使用带有空列表的head函数时如何获取null值

时间:2012-10-23 05:29:04

标签: 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

名为“way”的关系是可选的,因此当关系“way”不存在时,名为“consumeTime”的属性将是一个空列表。

查询结果为:

| 0 | “JGS”| [] |
| 1 | “SZX”| [3600] |

当我想使用带有“consumeTime”属性的head函数时,它会返回错误“Invalid query:head of empty list”。

我怎样才能得到这样的结果?

| 0 | “JGS”| null |
| 1 | “SZX”| 3600 |

1 个答案:

答案 0 :(得分:1)

这对于条件表达式来说是微不足道的,我认为这对于添加到Cypher非常重要:https://github.com/neo4j/community/issues/899

这是一个使用reduce的有点h​​acky查询,需要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)或条件表达式来检查空列表。