密码查询中的UNWIND后如何使用条件

时间:2019-02-25 15:51:16

标签: neo4j cypher graph-databases

我有这个密码查询

WITH ["first", "second"] AS things, -1 AS index
UNWIND range(0,size(things)-1) AS i  
Return things[i]

现在,我想检查是否存在things[i]等于“ first”的地方,然后返回它的索引,在这种情况下,返回0,其中“ first”的索引为0。但是我不知道该怎么写UNWIND之后的情况。

我试图在返回后添加条件:

WITH ["first", "second"] AS things, -1 AS index
UNWIND range(0,size(things)-1) AS i  
Return CASE WHEN things[i] = "first" THEN i AS index 

但是它不起作用,谢谢您的任何帮助。

1 个答案:

答案 0 :(得分:0)

此列表中只有一个“第一” ,但是列表中可能有多个“第一”的出现。

我在这里共享Cypher,以获取列表中任何字符串的第一个索引(此处为“ first”)

WITH ["first", "second"] AS things, -1 AS index
RETURN [x IN range(0,size(things)-1) WHERE things[x] = "second"][0] AS index

编辑:如果在列表中未找到字符串,则Cypher上方将返回 null 。 如果要在没有找到字符串的情况下为-1或任何其他值,请使用以下查询:

WITH ["first", "second"] AS things, -1 AS index
WITH [x IN range(0,size(things)-1) WHERE things[x] = "first"][0] AS index
RETURN  CASE WHEN index IS NOT NULL THEN index ELSE -1 END AS index