如何匹配cyphery中数组的值?

时间:2018-04-20 04:40:10

标签: neo4j

MATCH (s:Product {id:'4538875'})-[r]-> (o:Attributes) 
WHERE any(key in keys(o) WHERE o[key] contains 'type-c') 
return o

在此查询中,' o [key]'的值可以是数组或字符串。当它是一个数组时,"包含"关键字不会起作用。那么如何修改此查询以便能够匹配数组的每个值?

1 个答案:

答案 0 :(得分:4)

如果要将数组转换为字符串,那么REDUCE函数将帮助您(只需要一个不是数组的值来转换为数组):

WITH [1,2,3,'abc'] as array, 
     'abc' as string
RETURN REDUCE(res='', e in [] + array | res + e) as arrayToString,
       REDUCE(res='', e in [] + string | res + e) as testStringToString

或者您可以使用the toString function from the APOC library

RETURN apoc.convert.toString([1,2,3,'abc'])

<强>更新

如果不转换为字符串,您可以简单地遍历数组。例如:

MERGE (T1:Test {id: 1}) SET T1.value1 = 'abc'
MERGE (T2:Test {id: 2}) SET T2.value2 = ['def', 'xyzab']
WITH true AS tmp

MATCH (T:Test) 
WHERE ANY(k IN KEYS(T) 
  WHERE ANY(v IN [] + T[k] 
    WHERE toString(v) CONTAINS 'ab'
  )
) 
RETURN T