简体   繁体   English

如何返回与Neo4j具有多个关系的节点?

[英]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.

相关问题 找到具有多个传入关系的neo4j节点 - Find neo4j nodes with more than one incoming relationship 使用NEO4j显示具有多个关系的节点 - Show nodes with more than one relationship using NEO4j 如何返回neo4j中具有一定关系的节点,然后返回与第一个节点具有不同关系的节点? - How do I return nodes in neo4j that have a certain relationship, and then return nodes that have a different relationship with the first nodes? 仅返回具有多个(冗余)路径的节点到Neo4j(Cypher)中的不同节点 - Only return nodes with more than one (redundant) path to different nodes in Neo4j (Cypher) 使用cypherquery在neo4j中仅查找具有多个传入关系的节点 - Finding only nodes that have more than one incoming relationship in neo4j using cypherquery 不能在neo4j中的节点之间添加多个关系 - Cannot add more than one relationship between nodes in neo4j 如何在neo4j中返回节点的坐标 - How do I return coordinates of nodes in neo4j 带有空间的Neo4j:NotFoundException:多个关系 - Neo4j with spatial: NotFoundException: More than one relationship 在 Neo4j 中如何返回特定节点或与密码的关系? - In neo4j how to return specific nodes or relationship with cypher? 如何使用 cypher 删除 neo4j 中的多个关系? - How to delete more than one relationship in neo4j using cypher?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM