【问题标题】:Delete duplicate relationships with the same value of property saving other relationships with Cypher删除具有相同属性值的重复关系,保存与 Cypher 的其他关系
【发布时间】:2020-05-13 12:32:17
【问题描述】:

我需要使用 Cypher 删除具有相同属性值(年份)的重复关系,其中节点之间存在多个关系。举个例子,给定:

n1-[r{year:"2019"]->n2
n1-[r{year:"2019"]->n2
n1-[r{year:"2020"]->n2

我希望:

n1-[r{year:"2019"]->n2
n1-[r{year:"2020"]->n2

我不需要将属性组合到“2019,2020”,想保存多个不同属性(年份)的关系。

尝试为我的任务更改节点和关系的这些工作代码:

MATCH (n1),(n2)
WHERE n1.name = n2.name and id(n1) < id(n2)
WITH [n1,n2] as ns
CALL apoc.refactor.mergeNodes(ns) 
YIELD node
RETURN node


MATCH (n1) -[r]-> (n2) 
WITH n1,n2, collect(r) as rels 
CALL apoc.refactor.mergeRelationships(rels) 
YIELD rel 
RETURN rel

但是 WITH 之后我通常不能写部分

MATCH (n1) -[r1]-> (n2), (n1) -[r2]-> (n2) 
WHERE r1.year = r2.year and id(r1) < id(r2)
WITH #??? collect(?) as rels   
CALL apoc.refactor.mergeRelationships(rels) 
YIELD rel 
RETURN rel

【问题讨论】:

    标签: neo4j cypher


    【解决方案1】:

    我会改用分组。

    MATCH (:Foo) -[r:TYPE]->(:Bar)
    WITH r.year as year collect(r) as rels
    CALL apoc.refactor.mergeRelationships(rels) 
    YIELD rel 
    RETURN rel
    

    不知道你的意思:

    但是 WITH 之后我不能正常写部分

    【讨论】:

    • 谢谢,但有一些补充。如果有许多不同的节点,则会出现错误“所有关系必须具有相同的开始和结束节点”。在这种情况下,此代码将起作用:MATCH (a) -[r:TYPE]-&gt;(b) WITH r.year as year, [a,b] as ab, collect(r) as rels
    猜你喜欢
    • 1970-01-01
    • 2013-04-26
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    • 2020-06-22
    相关资源
    最近更新 更多