【问题标题】:Neo4j Cypher get all nodes in a linked listNeo4j Cypher 获取链表中的所有节点
【发布时间】:2020-02-04 17:46:12
【问题描述】:

如下图:

(Boxer)-[:starts]->(Round)-[:continues]->(Round)-[:continues]->(Round)-[:continues]->(Round)

如何让特定拳击手完成所有回合?

现在,我只能通过以下方式从所有拳击手那里获得所有回合: (我错过了第一轮,因为第一个关系是开始而不是继续。

MATCH (boxer:Boxer {id: 5})
MATCH ()-[:continues]->(round:Round)
 RETURN
  boxer {
   .*,
   rounds: collect(distinct round {
    .*
   })
  } as boxer

【问题讨论】:

    标签: graph neo4j cypher


    【解决方案1】:

    这可能对你有用:

    MATCH p = (boxer:Boxer)-[:starts]->()-[:continues*0..]->(lastRound)
    WHERE boxer.id = 5 AND NOT (lastRound)-[:continues]->()
    RETURN boxer {
       .*,
       rounds: NODES(p)[1..]
      } as boxer
    
    • variable-length relationship 模式 [:continues*0..] 使用 0 作为下限,以防回合只有一轮。
    • NOT (lastRound)-[:continues]->() 测试过滤在叶节点处结束的路径,因此 MATCH 仅获取整个回合的路径。
    • 返回的rounds 属性应包含回合中的所有回合。
    • 此查询假定startscontinues 关系类型始终具有Round 结束节点,因此为了提高效率,我们无需费心在MATCH 模式中指定这些节点标签。

    【讨论】:

    • 太棒了,按预期工作。您的解释和文档很有帮助。谢谢。
    猜你喜欢
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多