[英]Neo4j count is not predictable
Using the Preferential attachment example in the guides, I have a node C with degree 3 and node E with degree 1使用指南中的优先附件示例,我有一个度数为 3 的节点 C 和度数为 1 的节点 E
UNWIND [["A", "C"], ["A", "B"], ["B", "D"],
["B", "C"], ["B", "E"], ["C", "D"]] AS pair
MERGE (n1:Node {name: pair[0]})
MERGE (n2:Node {name: pair[1]})
MERGE (n1)-[:FRIENDS]-(n2)
and when I try a simple degree query for node EI get the correct answer.当我尝试对节点 EI 进行简单度数查询时,得到正确答案。 But when I add another node, the answer changes to 3但是当我添加另一个节点时,答案变为 3
MATCH (e:Node {name: 'E'})--(othere)
RETURN e,othere, count(othere)
returns 1 for count(othere)计数返回 1(其他)
MATCH (c:Node {name: 'C'})--(otherc)
MATCH (e:Node {name: 'E'})--(othere)
RETURN e,othere, count(othere)
returns 3 for count(othere).计数(其他)返回 3。 Why should this be?为什么会这样?
In your last query's RETURN
clause, the COUNT
aggregating function counts the number of result rows that have the same e
and othere
values.在最后一个查询的RETURN
子句中, 聚合 function的COUNT
计算具有相同e
和othere
值的结果行数。 With your sample data, there are 3 such result rows.使用您的样本数据,有 3 个这样的结果行。
Here is one way to get a correct count of the number of relationships between e
and othere
:这是正确计算e
和othere
之间关系数量的一种方法:
MATCH (c:Node {name: 'C'})--(otherc)
MATCH (e:Node {name: 'E'})-[r]-(othere)
RETURN e, othere, COUNT(DISTINCT r)
[DISCUSSION] [讨论]
In general, there can be any number of relationships between any 2 nodes.通常,任何 2 个节点之间可以有任意数量的关系。 So, to make this a more general discussion, suppose the "E" node has 2 relationships to the "B" node (and no other relationships).因此,为了进行更一般的讨论,假设“E”节点与“B”节点有 2 个关系(并且没有其他关系)。
My query would return a correct COUNT
(degree) of 2. Remember, the degree of a node is its number of relationships .我的查询将返回正确的COUNT
(degree) 2。请记住,节点的度数是它的关系数。
A similar-looking query that returned COUNT(DISTINCT othere)
instead of COUNT(DISTINCT r)
would return 1, which is incorrect.返回COUNT(DISTINCT othere)
而不是COUNT(DISTINCT r)
的类似查询将返回 1,这是不正确的。
Your second query's return clause ( RETURN e, othere, COUNT(othere)
) clause would return a COUNT
of 6 (because there'd be 6 result rows with othere
).您的第二个查询的 return 子句( RETURN e, othere, COUNT(othere)
)子句将返回COUNT
6(因为othere
会有 6 个结果行)。
I hope this helps to make clear why I used COUNT(DISTINCT r)
.我希望这有助于弄清楚我为什么使用COUNT(DISTINCT r)
。 You should also read the aggregating function documentation carefully.您还应该仔细阅读汇总的 function文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.