![](/img/trans.png)
[英]How do I create a constraint on the number of relationships between two nodes in Neo4j
[英]How do i get number of same relationships between two nodes with Cypher
我使用以下代码片段重新创建了数据集:
CREATE
(:Person {name: 'Jan'})-[:Father]->
(:Person {name: 'Mirko'})-[:Father]->
(:Person {name: 'Cupko'})-[:Father]->
(:Person {name: 'Borko'})
MATCH p=(b:Person {name: 'Borko'})<-[:Father*]-(n)
RETURN n, length(p)
返回:
╒════════════════╤═══════════╕
│"n" │"length(p)"│
╞════════════════╪═══════════╡
│{"name":"Cupko"}│1 │
├────────────────┼───────────┤
│{"name":"Mirko"}│2 │
├────────────────┼───────────┤
│{"name":"Jan"} │3 │
└────────────────┴───────────┘
对于第二部分,可以使用relationships
函数:
MATCH p=(b:Person {name: 'Borko'})<-[:Father|Mother*]-(n)
WITH p, relationships(p) AS rels, n
RETURN n, length(p) AS hops, type(rels[length(rels)-1]) AS type
返回:
╒════════════════╤══════╤════════╕
│"n" │"hops"│"type" │
╞════════════════╪══════╪════════╡
│{"name":"Cupko"}│1 │"Father"│
├────────────────┼──────┼────────┤
│{"name":"Mirko"}│2 │"Father"│
├────────────────┼──────┼────────┤
│{"name":"Jan"} │3 │"Father"│
└────────────────┴──────┴────────┘
当然,您可以使用大小写表达式将关系的名称转换为适当的类型,例如CASE type(rels[length(rels)-1]) WHEN 'Father' THEN 'Dad' WHEN 'Mother' THEN 'Mom' END AS role
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.