简体   繁体   中英

UNWIND is not returning in neo4j

Consider the follwing CQL Query

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

When I excute the query, It should return rows and count (according to query). But instead of returning rows, count it's giving result statement.

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

Is there any problem with query structure or problem with improper use of UNWIND clause.

If other_rows is null or empty, UNWIND will not produce any rows.

You could solve it with:

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

Addition to Micheael Hunger Answer

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

As I am operating on values of array, instead of null, I need to add extra condition so that it can't throw any error mesaages.

This apllies for both cases (other_rows, null_less_rows)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM