我目前正在開發一個新的應用程序,我需要管理位置(大洲>國家>地區>城市)。解析樹結果/得到兒童
我從Neo4j的使用暗號嘗試(沒有成功),以獲得一棵樹直接:
MATCH p = (r:Location)-[:CONTAINS*]->()
WHERE r.category='continent'
RETURN p AS path
現在,我想分析這些DATAS,這樣我就可以做到這一點的方法:
MATCH (r:Location)-[:CONTAINS]->(r2)
WHERE r.category='continent'
OPTIONAL MATCH (r2)-[:CONTAINS]->(r3)
OPTIONAL MATCH (r3)-[:CONTAINS]->(r4)
OPTIONAL MATCH (r4)-[:CONTAINS]->(r5)
OPTIONAL MATCH (r6)-[:CONTAINS]->(r6)
RETURN r.name, r.category, r2.name, r2.category, r3.name, r3.category, r4.name,r4.category, r5.name, r5.category, r6.name, r6.category
但這不是我等待的迴應,我不喜歡這個解決方案,因爲最多有6個關係。如果我這樣做,我冒險破壞樹。
[ { 'r.name': 'Europe','r.category': 'continent','r2.name': 'Germany','r2.category': 'country','r3.name': null,'r3.category': null,'r4.name': null,'r4.category': null,'r5.name': null,'r5.category': null,'r6.name': null,'r6.category': null },
{ 'r.name': 'Europe','r.category': 'continent','r2.name': 'France','r2.category': 'country','r3.name': 'Ile de France','r3.category': 'region','r4.name': 'Paris','r4.category': 'city','r5.name': null,'r5.category': null,'r6.name': null,'r6.category': null },
{ 'r.name': 'Europe','r.category': 'continent','r2.name': 'France','r2.category': 'country','r3.name': 'Ile de France','r3.category': 'region','r4.name': 'Versailles','r4.category': 'city','r5.name': null,'r5.category': null,'r6.name': null,'r6.category': null },
{ 'r.name': 'Europe','r.category': 'continent','r2.name': 'France','r2.category': 'country','r3.name': 'Ile de France','r3.category': 'region','r4.name': 'Montreuil','r4.category': 'city','r5.name': null,'r5.category': null,'r6.name': null,'r6.category': null },
{ 'r.name': 'Europe','r.category': 'continent','r2.name': 'Belgium','r2.category': 'country','r3.name': null,'r3.category': null,'r4.name': null,'r4.category': null,'r5.name': null,'r5.category': null,'r6.name': null,'r6.category': null } ]
有沒有一種方法來分析結果/改變我的查詢以獲得一個JSON格式與「兒童」樹的那樣:
[{"name":"Africa"},{"name":"Europe","childrens":[{"name":"France"},{"name":"Germany"},{"name":"Belgium","childrens":[{"name":"Brussels Capitale","childrens":[{"name":"Brussels Capitale"}]}]}]}]
感謝您的幫助!
Fred。
邑,謝謝......這是最接近的解決方案,我發現......但我不能(現在)得到(l:Location)WHERE l.category ='continent'OPTIONAL MATCH(l) - [:CONTAINS] - >(l2)我們使用此查詢: 'MATCH可選匹配(l2) - [:CONTAINS] - >(l3)可選匹配(l3) - [:CONTAINS] - >(l4)RETURN {name:l.name,children:COLLECT(distinct {name:l2.name,兒童:COLLECT(不同的{name:l3.name,children:COLLECT(distinct {name:l4.name})})})}' 但是我得到這個錯誤: 錯誤:不能使用集合函數。' – frednotet
和這一個,我得到一個「未知的錯誤」: '匹配(l:位置)哪裏l。(l2) - [:CONTAINS] - (l3) WITH L AS l,l2 AS l2,l3 AS l3,COLLECT(l3) OPTIONAL MATCH(l) - [:CONTAINS] - (l2) (DISTINCT l2.name)AS children WITH l AS l,l2 AS l2,l3 AS l3,children AS children,COLLECT(DISTINCT l3.name)AS children2 RETURN {name:l,children:{name:children,children :{name:children2}}}' – frednotet