简体   繁体   English

密码NOT IN查询(可选匹配)

[英]cypher NOT IN query with Optional Match

NOT RELEVANT - SKIP TO Important Edit. 不相关-跳至重要编辑。

I have the following query: 我有以下查询:

MATCH (n)
WHERE (n:person) AND n.id in ['af97ab48544b'] // id is our system identifier
OPTIONAL MATCH (n)-[r:friend|connected|owner]-(m)
WHERE (m:person OR m:dog OR m:cat)
RETURN n,r,m

This query returns all the persons, dogs and cats that have a relationship with a specific person. 该查询返回与特定人有关系的所有人,狗和猫。 I would like to turn it over to receive all the nodes & relationships that NOT includes in this query results. 我想将其翻转以接收此查询结果中不包括的所有节点和关系。

If it was SQL it would be 如果是SQL,那就是

select * from graph where id NOT IN (my_query)

I think that the OPTIONAL MATCH is the problematic part. 我认为可选匹配是有问题的部分。 I How can I do it? 我该怎么办? Any advice? 有什么建议吗?

Thanks. 谢谢。

-- Important Edit -- -重要编辑-

Hey guys, sorry for changing my question but my requirements has been changed. 大家好,抱歉更改我的问题,但我的要求已更改。 I need to get the entire graph (all nodes and relationships) connected and disconnected except specific nodes by ids. 我需要通过ID获取和断开整个图(所有节点和关系)的连接和断开连接。 The following query is working but only for single id, in case of more ids it isn't working. 以下查询有效,但仅适用于单个ID,如果存在更多ID,则该查询不起作用。

MATCH (n) WHERE (n:person)
OPTIONAL MATCH (n)-[r:friend|connected|owner]-(m) WHERE (m:person OR m:dog OR m:cat)
WITH n,r,m
MATCH (excludeNode) WHERE excludeNode.id IN ['af97ab48544b']
WITH n,r,m,excludeNode WHERE NOT n.id = excludeNode.id AND (NOT m.id = excludeNode.id OR m is null)
RETURN n,m,r

Alternatively I tried simpler query: 另外,我尝试了更简单的查询:

MATCH (n) WHERE (n:person) AND NOT n.id IN ['af97ab48544b'] return n

But this one does not returns the relationships (remember I need disconnected nodes also). 但这并不会返回关系(记住我也需要断开连接的节点)。

How can I get the entire graph exclude specific nodes? 如何获得整个图排除特定节点? That includes nodes and relationships, connected nodes and disconnected as well. 其中包括节点和关系,已连接的节点以及未连接的节点。

You've gotta switch the 'perspective' of your query... start by looping over every node, then prune the ones that connect to your person. 您必须切换查询的“视角” ...首先遍历每个节点,然后修剪连接到您个人的节点。

MATCH (bad:person) WHERE bad.id IN ['af97ab48544b']
WITH COLLECT(bad) AS bads
MATCH path = (n:person) - [r:friend|:connected|:owner] -> (m)
WHERE n._id = '' AND (m:person OR m:cat OR m:dog) AND NOT ANY(bad IN bads WHERE bad IN NODES(path))
RETURN path

That said, this is a problem much more suited to SQL than to a graph. 也就是说,与图相比,这是一个更适合SQL的问题。 Any time you have to loop over every node with a label, you're in relational territory, the graph will be less efficient. 每当您必须遍历带有标签的每个节点时,您就处在关系区域中,该图的效率就会降低。

try this: 尝试这个:

match (n) where not n.id = 'id to remove' optional match (n)-[r]-(m) where not n.id in ['id to remove'] and not m.id in ['id to remove'] return n,r,m 匹配(n),其中不是n.id ='要删除的ID'可选匹配(n)-[r]-(m)其中,在['id要删除的ID]中不是n.id,在['id]中不是m.id删除']返回n,r,m

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM