使用Cypher合并两个语句时的副作用 [英] Side effects while combining two statements using Cypher

查看:77
本文介绍了使用Cypher合并两个语句时的副作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了以下示例案例:

I created the following example case:

MERGE (p1:C5{userId:'1234'})
mERGE (p2:C5{userId:'555'})
MERGE (p3:C5{userId:'1234'})
mERGE (p4:C5{userId:'6969'})
MERGE (p1)-[r1:follow]->(p2) 
MERGE (p2)-[t1:follow]->(p1)
MERGE (p3)-[r2:follow]->(p4) 
MERGE (p4)-[t2:follow]->(p3)
SET r1.type='mirror',
t1.type='real',
r2.type='real',
t2.type='mirror'

我正在尝试创建一条语句,仅根据以下条件从给定Node(通过userId prop)删除关系:

I am trying to create a statement which delete relationships from a given Node(by userId prop) only by this criteria:

  1. 对于给定的节点,传入关系(follow.type)='mirror'
  2. 对于给定的节点,传出关系(follow.type)='real'

您无需同时拥有两者即可申请.每个规则都是个人的,我只是想将其结合在同一条语句上.

You dont need to have both in order to apply. each rules is individual I am just trying to combine it on the same statement.

所以我设法写了一些东西,但是有副作用. 它会删除所有关系,并且不会考虑我提到的规则:

So I managed to write something but I have side effects. It deletes all relationshions and it doesnt consider the rules I mentioned:

MATCH (n:C5 { userId: '1234' })<-[r]-(),(g:C5{userId:'1234'})-[y]->() 
        WHERE r.type='mirror' or y.type='real'
        DELETE  r,y

如果您检查此代码,您将看到所有关系都消失了.

if you check this code you'll see all relationships are gone.

我希望仅看到节点(1234)和节点(6969)之间的关系消失,因为传出和传入关系都分别应用了我的规则.

I expected to see only the relationship btw node(1234) and node(6969) to be gone coz both outgoing and incoming relationships individually apply my rules.

现在,如果我将此查询分为2个可以按预期工作的语句:

Now if I split this query into 2 statements that working as expected:

MATCH (n:C5 { userId: '1234' })<-[r]-() 
            WHERE r.type='mirror' 
            DELETE  r


MATCH (g:C5{userId:'1234'})-[y]->() 
            WHERE y.type='real'
            DELETE  y

但是我确实想将其合并为一个. 谢谢, 射线.

But I do want to merge it into one. Thank you, ray.

评论后的新案例:

MERGE (p1:C4{userId:'1234'})
mERGE (p2:C4{userId:'555'})
MERGE (p1)-[r1:follow]->(p2) 
MERGE (p2)-[t1:follow]->(p1) 
SET r1.type='real',
t1.type='mirror'

执行以下操作:

match (n:C4{userId:'1234'})-[y]->(g:C4{userId:'555'}),
      (n:C4{userId:'1234'})<-[r]-(g:C4{userId:'555'})
WHERE y.type='real' and r.type='real' 
set y.type='mirror'
with y,r
match (n:C4{userId:'1234'})-[x]->(g:C4{userId:'555'}),
      (n:C4{userId:'1234'})<-[z]-(g:C4{userId:'555'})
WHERE x.type='real' and z.type='mirror' 
delete x,z

我尝试在此处创建2个条件.如果应用了1个条件,我只是设置了,如果应用了2个条件,我就是删除了

I tried to create here 2 conditions. if 1 condition applied I just do set if 2 condition applied I am doing delete

我的案件现在应触发确实删除的第二部分.但那部分从未触发,我受到了0条线的影响.

My case now should trigger the second part which does delete. but that part never triggered I get 0 lines affected.

推荐答案

这里的问题是"OR"部分.

The problem here is the "OR" part.

我的意思是,在您的查询中,您获得2个关系,并且如果第一个的类型为镜像"或第二个的类型为真实",则将两者都删除.

I mean, in your query, you get 2 relationships, and if the type of the first is "mirror" OR the type of the second is "real" you delete both.

因此,如果满足您指定的条件之一,则将删除两个关系.

So if one of the conditions you specified is met, both relationships are deleted.

这也是为什么拆分查询按预期运行的原因.

That's also why the splited query is working as expected.

我认为您正在寻找这个:

I think you are looking for this:

MATCH (n:C5 { userId: '1234' })<-[r]-()
WHERE r.type='mirror' 
WITH r
MATCH (n:C5 { userId: '1234' })-[y]->()
WHERE y.type='real'
DELETE r,y

由于我在DELETE部分遇到了CASE的问题,所以我做到了这一点,使用WITH将r传递给第二个匹配项,然后删除满足要求的关系.

Since I got an issue with CASE in DELETE part, I made this one, using WITH to pass r to a second match, and then deleting relationships who met requirements.

如果我使用CASE找到解决方案,则此答案将被编辑.

If I find the solution using CASE, this answer will be edited.

这篇关于使用Cypher合并两个语句时的副作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆