[英]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.