是否删除了每个关系的可选关系?

时间:2021-06-23 18:01:27

In Neo4j 2.0.0-M05 i used to do

在Neo4j 2.0 - m05中,我曾经做过

START n=node(*) MATCH n-[r?]-() WHERE ID(n) <> 0 AND ID(n) <> 1 DELETE n, r

To delete the entire database except node 0 and 1. Now in Noe4j 2.0.0-RC1 I can not use the question mark anymore. But instead i have to use the OPTIONAL keyword like this:

删除除节点0和1之外的整个数据库。现在在Noe4j 2.0.0-RC1中,我不能再使用问号了。但是我必须使用这样的可选关键字:

START n=node(*) OPTIONAL MATCH n-[r]-() WHERE ID(n) <> 0 AND ID(n) <> 1 DELETE n, r

Problem 1

It matches node 1 (node 0 was not present in the database at the moment of the query)

它匹配节点1(查询时数据库中不存在节点0)

neo4j-sh (?)$ START n=node(*) OPTIONAL MATCH n-[r]-() WHERE ID(n) <> 1 RETURN n, r;
+--------------------+
| n         | r      |
+--------------------+
| Node[1]{} | <null> |
+--------------------+

This is of course not a problem only for this one query, but throughout the application where I need to use similar queries.

当然,这不仅仅是一个查询的问题,而是整个应用程序中需要使用类似查询的问题。

Problem 2

As it looks to me this introduced a serious design flaw. Maybe my reasoning is off here, and also I could not test it yet because of Problem 1. But the OPTIONAL keyword severely limits the possibilities to make queries. As written in the announcement here http://blog.neo4j.org/2013/11/neo4j-200-rc1-final-preparations.html OPTIONAL can be compared to an OUTER JOIN. To keep up with this anology hopping from node to node would be like adding an extra join.

在我看来,这带来了一个严重的设计缺陷。也许我的推理是错误的,而且由于问题1,我还不能测试它。但是可选关键字严重限制了查询的可能性。正如这里的声明中所写的:http://blog.neo4j.org/2013/11/neo4j-200-rc1-final-preparations.html OPTIONAL可以与外部连接相比较。为了跟上这种从一个节点跳到另一个节点就像添加一个额外的连接。

So before

所以之前

MATCH (a)-[x]-(b)-[y?]-(c) RETURN a, b, c

Would be like doing

就像做

SELECT a, b, c FROM ??
INNER JOIN x ON ??
LEFT OUTER JOIN y ON ??

(question marks in place because cypher does not translate to SQL)
But not with the OPTIONAL keyword either you make all the joins OUTER JOIN or all the joins INNER JOIN (without the optional keyword). My application doesn't work without mixed inner/outer join queries. To me it's very basic to make the relational optional PER HOP. So either I have to stay at M06 (the latest version to support the question mark) or try another database.

(因为cypher不翻译成SQL,所以有问号)但是不能使用可选关键字:要么将所有的JOIN外部连接或所有的JOIN内部连接(没有可选关键字)。如果没有混合的内部/外部连接查询,我的应用程序就不能工作。对我来说,让关系在每跳中可选是非常基本的。因此,我要么继续使用M06(支持问号的最新版本),要么尝试另一个数据库。

Signed,

签署,

Desperate developer.

绝望的开发人员。

1 个解决方案

#1


2  

Please provide some sample data and expected results. I'm pretty sure your two queries can be rewritten with optional match as below.

请提供一些样本数据和预期结果。我确信您的两个查询可以用可选匹配项重写,如下所示。

The reason your WHERE clause acts weirdly, is because the WHERE directly following the OPTIONAL MATCH only gets run when the OPTIONAL MATCH matches something, as a filter for that OPTIONAL MATCH. So in this case, you're finding n (id = 1), it's not matching n-[r]-(), since n doesn't have a relationship, and then it's returning n. You need to filter your n first, although I recommend using just MATCH instead of START (in this case it doesn't matter, but usually MATCH is better).

WHERE子句之所以奇怪,是因为直接跟随可选匹配项的WHERE只在可选匹配项匹配时运行,作为可选匹配项的过滤器。所以在这种情况下,你发现n(id = 1),它不是匹配的n -(r)-(),因为n没有关系,然后返回n。你需要过滤n第一,虽然我建议使用只是比赛,而不是开始(在这种情况下没关系,但通常匹配更好)。

For your #1, you need to do this query instead:

对于您的第一条,您需要执行以下查询:

MATCH (n)
WHERE ID(n) <> 1   
OPTIONAL MATCH n-[r]-() 
RETURN n, r;

For your #2:

为你的# 2:

MATCH (a)-[x]-(b)
OPTIONAL MATCH (b)-[y]-(c)
RETURN a, b, c // c will be null if it doesn't exist

#1


2  

Please provide some sample data and expected results. I'm pretty sure your two queries can be rewritten with optional match as below.

请提供一些样本数据和预期结果。我确信您的两个查询可以用可选匹配项重写,如下所示。

The reason your WHERE clause acts weirdly, is because the WHERE directly following the OPTIONAL MATCH only gets run when the OPTIONAL MATCH matches something, as a filter for that OPTIONAL MATCH. So in this case, you're finding n (id = 1), it's not matching n-[r]-(), since n doesn't have a relationship, and then it's returning n. You need to filter your n first, although I recommend using just MATCH instead of START (in this case it doesn't matter, but usually MATCH is better).

WHERE子句之所以奇怪,是因为直接跟随可选匹配项的WHERE只在可选匹配项匹配时运行,作为可选匹配项的过滤器。所以在这种情况下,你发现n(id = 1),它不是匹配的n -(r)-(),因为n没有关系,然后返回n。你需要过滤n第一,虽然我建议使用只是比赛,而不是开始(在这种情况下没关系,但通常匹配更好)。

For your #1, you need to do this query instead:

对于您的第一条,您需要执行以下查询:

MATCH (n)
WHERE ID(n) <> 1   
OPTIONAL MATCH n-[r]-() 
RETURN n, r;

For your #2:

为你的# 2:

MATCH (a)-[x]-(b)
OPTIONAL MATCH (b)-[y]-(c)
RETURN a, b, c // c will be null if it doesn't exist