optional match未返回null值
发布于 5 年前 作者 haidfs 2212 次浏览 来自 问答

原本语句是希望找到符合条件的路径,找不到的话希望Cypher能返回一个空值 optional match p=(src:L2SUBIF{name:‘Et@leaf3’})-[r:HAS|L2|TUNNEL*2…4]-(dst:L2SUBIF{name:‘Eth-002@leaf3’}) unwind nodes§ as n with p, size(collect(distinct n))as nLength,src,dst,r where condition1 and cond2 and con3 and con4 return p,r 但是上面这样的语句只是提示(no changes, no records), 该怎么返回一个null。或者说Cypher或Java能否以返回值的形式(不存在为null)判断某条路径是否存在?我试着用了apoc.cypher.run()==null,并不成功。希望大家指点一二

4 回复

unwind nodes§ as n 你用了unwind。这里会产生null 你可以unwind 的时候加case 试一试

To avoid inadvertently using UNWIND on an empty list, CASE may be used to replace an empty list with a null: WITH [] AS list UNWIND CASE WHEN list = [] THEN [null] ELSE list END AS emptylist RETURN emptylist 116

optional match(n:idcard{idcard:idkey}) with idkey, [(n)-[:parent_rel]-(m:idcard)|m] as list with idkey , case when list<>[] then apoc.coll.toSet(list) else [] end as cur_app_id_withparent with idkey, case when cur_app_id_withparent=[] then [null] else cur_app_id_withparent end as cur_app_id_withparenta unwind cur_app_id_withparenta as cha unwind cur_app_id_withparenta as chb with idkey,[(cha)-[r]-(chb)|type® ] as list WITH idkey, filter(n IN list WHERE n<>[‘relatives_rel’] and n<>[] and n is not null ) AS list with idkey,case when count(list)>0 then 1 else 0 end as cur_relation_btw_parent return idkey,‘cur_relation_btw_parent’ as type, cur_relation_btw_parent as flag demo用法,不要用where条件,注意unwind条件

@bingo 感谢。我后面也发现了是unwind的问题,最后我的解决方式是用Java和Cypher交互,在apoc里面,apoc.cypher.run的返回结果是用Stream封装的,我用Stream的api进行了判断。

回到顶部