[英]How do I return nodes with more than one relationship with Neo4j?
I want to return a list of 'Group' nodes which have more than one 'Thing' node belonging to each of them. 我想返回一个“组”节点的列表,其中有多个“物”节点属于每个节点。 There are some duplicate relationships, so I'm counting DISTINCT Things. 有一些重复的关系,所以我算是DISTINCT Things。 This query seems to work fine so far: 到目前为止,该查询似乎运行良好:
MATCH (n:Thing)-[r:BELONGS_TO]-(g:Group)
WITH g, count(DISTINCT n) as thing_cnt
WHERE thing_cnt > 1
RETURN g, thing_cnt ORDER BY thing_cnt
I'd like to be able to visualise all of these Group nodes and the Things that belong to them, so I tried this: 我希望能够可视化所有这些组节点和属于它们的事物,因此我尝试了以下操作:
MATCH (n:Thing)-[r:BELONGS_TO]-(g:Group)
WITH g, count(DISTINCT n) as thing_cnt
WHERE thing_cnt > 1
RETURN n, g
But I get an error telling me that n is not defined. 但是我收到一个错误消息,告诉我n没有定义。 Can anyone suggest a way to do this? 有人可以建议一种方法吗? Thanks! 谢谢!
The WITH
clause needs to include n
: WITH
子句需要包含n
:
MATCH (n:Thing)-[r:BELONGS_TO]-(g:Group)
WITH n, g, count(DISTINCT n) as thing_cnt
WHERE thing_cnt > 1
RETURN n, g
As an aside, you should always specify the directionality of your relationships, if possible. 顺便说一句,如果可能的话,您应该始终指定关系的方向性。 For example, (n:Thing)-[r:BELONGS_TO]->(g:Group)
. 例如, (n:Thing)-[r:BELONGS_TO]->(g:Group)
。 At the very least, that improves the readability and hum-understandability of your graph model. 至少,这可以提高图形模型的可读性和嗡嗡声。 Also, it could speed up you query as well. 同样,它也可以加快查询速度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.