Cypher中意外的行为结合了集合 [英] Unexpected behavior combining collections in Cypher

查看:123
本文介绍了Cypher中意外的行为结合了集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 http://console.neo4j.org 作为沙箱,我遇到了以下意外行为:

Using http://console.neo4j.org as a sandbox, I have come across the following unexpected behavior:

声明1-返回1行,其中包含一个包含Neo Node的集合

MATCH (n:Crew) 
WHERE n.name="Neo" 
WITH COLLECT(n) AS c1
WITH c1+[] AS c2
RETURN c2

声明2-返回0行(意外)

MATCH (n:Crew) 
WHERE n.name="Neo" 
WITH COLLECT(n) AS c1
MATCH (n:Crew) 
WHERE n.name="NoOne"
WITH c1+COLLECT(n) AS c2
RETURN c2

声明3-返回1行,其中包含一个空集合

MATCH (n:Crew) 
WHERE n.name="NoOne"
WITH COLLECT(n) AS c1
RETURN c1

我看不到为什么 Statement 2 不能返回与 Statement 1 相同的结果,因为它应该返回包含Neo节点的集合,就像 >声明1 .
声明3 显示声明2 中的第二个MATCH应该导致一个空集合.

I fail to see why Statement 2 is not returning the same result as Statement 1, because it should return a collection containing the Neo node, just like in Statement 1.
Statement 3 shows that the second MATCH in Statement 2 should be resulting in an empty collection.

在Cypher中这种行为是预期的吗?如果是这样,我很乐意为您提供一个简短的解释,以帮助我理解这种行为.

Is this behavior expected in Cypher? If that's the case, I'd be happy about a small explanation to help me understand this behavior.

推荐答案

我以前遇到过这种确切的行为,这非常令人沮丧.问题在于查询2中的第二个MATCH子句:如果现有结果行(在本例中为带有c1的单行)不返回任何MATCH的结果,则该行将被完全删除在该MATCH子句之后,即使 单独MATCH(没有预先存在的结果行)返回一个空集合.如果将其转换为OPTIONAL MATCH,则可以在没有匹配项的情况下保留结果行.

I've run into this exact behavior before, and it is very frustrating. The issue is with the second MATCH clause in Query 2: if an existing result row (in this case, your single row with c1) doesn't return any results for a MATCH, that row will be dropped completely after that MATCH clause, even though that MATCH on its own (without the pre-existing result row) returns an empty collection. If you convert it to an OPTIONAL MATCH you'll be able to keep your result row when there are no matches.

更新:请参见下面的更全面的分析,但tl,dr是语句2 中的第二个COLLECT(n)确实返回空列表,就像语句3中一样;但是,整个子句WITH c1+COLLECT(n) AS c2不返回任何行,因为在第二个MATCH之后没有具有c1值的行.

UPDATE: See below for a more thorough analysis, but the tl,dr is that the second COLLECT(n) in Statement 2 does return an empty list, just like in Statement 3; however, the whole clause WITH c1+COLLECT(n) AS c2 returns no rows, because there are no rows with a c1 value after the second MATCH.

这篇关于Cypher中意外的行为结合了集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆