[英]Neo4j cypher query to get all nodes without a relationship to specific other node
I use Neo4j 2.3.1 and looking I am looking for a cypher query which gives me all nodes in the database which are not connected to the specific other node. 我使用Neo4j 2.3.1,我正在寻找一个密码查询,它给了我数据库中没有连接到特定其他节点的所有节点。 My Tree looks like this:
我的树看起来像这样:
I know the red one and want to have a query which gives me the green ones. 我知道红色的那个,想要一个给我绿色的查询。 All of them do have the same relationship.
他们都有相同的关系。
EDIT: My question is misleading worded, so: What I want (as shown in the image) all nodes which are "above" a specific node and also their childs. 编辑:我的问题是误导措辞,所以:我想要的(如图所示)所有节点都在特定节点“上方”以及他们的孩子。
This should work: 这应该工作:
MATCH (red)<-[*]-(parent)-[*0..10]->(children)
WHERE red.id = xxx
RETURN parent, children
Find all of the parents of the red node and all children of the parents. 找到红色节点的所有父母和父母的所有孩子。
In general, a graph DB can have multiple disjoint subgraphs (which do not even have to be trees). 通常,图形DB可以具有多个不相交的子图(甚至不必是树)。 For example, suppose there are additional nodes that are not connected in any way to your subgraph.
例如,假设有其他节点未以任何方式连接到子图。
Here is one way you can get all nodes that are not connected to a specified node via forward REL
relationships. 这是通过前向
REL
关系获得未连接到指定节点的所有节点的一种方法。 I assume that the interesting nodes all have the same label ( Foo
) and that there are other nodes without that label. 我假设有趣的节点都有相同的标签(
Foo
),并且还有其他没有该标签的节点。
MATCH (n:Foo { id: 123 })-[:REL*]->(m:Foo)
WITH (COLLECT(DISTINCT m) + n) AS not_wanted
MATCH (x:Foo)
WHERE NOT x IN not_wanted
RETURN x;
Note: This query can take a very long time (or run out of memory), depending on big the "tree" rooted at n
is and how many nodes there are in the DB. 注意:此查询可能需要很长时间(或耗尽内存),具体取决于以
n
为根的“树”以及数据库中有多少个节点。 You should omit the node labels that do not help you filter out anything. 您应该省略不帮助您过滤掉任何内容的节点标签。
Depends. 要看。 Assuming that all of your node labels are the same, this should work:
假设所有节点标签都相同,这应该有效:
MATCH (a:circle)-[r]->(b:circle)
WHERE a.colour <> 'Red' AND b.colour <> 'Red'
RETURN a,b
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.