2017-04-03 81 views
1

返回考慮follwing CQL查詢UNWIND不Neo4j的

MATCH (n:Label1) WITH n 
OPTIONAL MATCH (n)-[r:REL_1]-(:Label2 {id: 5}) 
WHERE r is NULL OR r.d < 12345 OR (r.d = 12345 OR r.c < 2) 
WITH n,r LIMIT 100 
WITH COLLECT({n: n, r: r}) AS rows 
MERGE (c:Label2 {id: 5}) 
WITH c, 
[b IN rows WHERE b.r.d IS NULL OR b.r.d < 12345] AS null_less_rows, 
[c IN rows WHERE (c.r.d = 12345 AND c.r.c < 2)] AS other_rows 
WITH null_less_rows, other_rows, c, null_less_rows+other_rows AS rows, size(null_less_rows+other_rows) AS count 
UNWIND null_less_rows AS null_less_row 
MERGE(s:Label1 {id: null_less_row.n.id}) 
MERGE(s)-[:REL_1 {d: 12345, c: 1}]->(c) 
WITH DISTINCT other_rows, c, rows, count 
UNWIND other_rows AS other_row 
MATCH(s:Label1 {id: other_row.n.id})-[str:REL_1]->(c) SET str.c = str.c + 1 
WITH rows, count 
RETURN rows, count 

當我EXCUTE查詢,它應該返回行(按查詢)計數。但是不要返回行,請將其計爲結果語句。

Set 200 properties, created 100 relationships, statement completed in 13 ms. 

查詢結構是否存在任何問題或使用UNWIND子句時出現問題。

+0

嘗試插入'RETURN other_rows,行,最後'MERGE'後count'(和評論畢竟)。結果會是什麼?附:對於未來:如果您要求幫助理解複雜的查詢 - 舉例說明可以檢查此查詢的輸入數據。 http://stackoverflow.com/help/mcve –

回答

0

加成Micheael飢餓應答

UNWIND (CASE other_rows WHEN [] then [{n:{id: -2}}] else other_rows end) AS other_row 

由於我對數組的值進行操作,而不是空,我需要添加額外的條件,使它不能拋出任何錯誤mesaages。

這apllies兩種情況下(other_rows,null_less_rows)

0

如果other_rows爲null或爲空,UNWIND將不會產生任何行。

您可以用解決它:

UNWIND case coalesce(size(other_rows),0) when 0 then [null] else other_rows end 
    as other_row