我已經follwing簡單的圖形:獲取葉的第一根中的Neo4j
CREATE (:leaf)<-[:rel]-(:nonleaf)<-[:rel]-(:nonleaf)<-[:rel]-(:nonleaf)<-[:rel]-(r1:nonleaf{root:true})<-[:rel]-(r2:nonleaf{root:true})
我想開始從(:leaf)
與root:true
在其上設置始祖。那是我想要得到r1
。爲此,我寫了下面的暗號:
MATCH (:leaf)<-[*]-(m)<-[*]-(r:nonleaf{root:true}) WHERE m.root<>true OR NOT exists(m.root)
RETURN r
但它同時返回(r1)
和(r2)
。同樣發生的後續密碼:
MATCH shortestPath((l:leaf)<-[*]-(r:nonleaf{root:true}))
RETURN r
這是怎麼回事?
更新
好的思維更後,點擊它給我記住,(r2)
也被返回因爲路徑從(:leaf)
到(r2)
,有這應該已經單擊以我早期沒有root
財產對他們(設置節點,非常明顯,微妙的解釋錯誤)。換句話說,如果「至少有一個m
」條件成立:m.root<>true OR NOT exists(m.root)
,則返回(:nonleaf{root:true})
。這裏的要求是條件應該對路徑上的「全部m
s」,「不是至少一個m
」有效。現在仍然要弄清楚如何把它放在密碼中,而我對密碼的掌握並不是那麼緊密......
我想這會爲失敗:'CREATE(:葉)< - [:REL] - (:非葉)< - [:相對] - (:非葉)< - [:REL] - (:非葉)< - [:相對] - (R 1:非葉{根:真})< - [:相對] - (:非葉)< - [:REL] - (R2:非葉根{:真})'。它會返回'(r1)'和'(r2)'。對? – Mahesha999
你試過了嗎? –
是的。看起來你認爲下面不會發生:'(nonroot)< - (root)< - (nonroot)< - (root)'。在這種情況下,兩個根都會返回,但我只需要最左側的根。那就是我用新的CREATE來評論的。試試這些:CREATE(:leaf {id:'leaf1'})< - [:rel] - (:nonleaf)< - [:rel] - (:nonleaf)< - [:rel] - (:nonleaf)< - [:rel] - (r1:nonleaf {root:true})< - [:rel] - (:nonleaf)< - [:rel] - (r2:nonleaf {root:true})' ::葉{{:葉1'})< - [*] - (r:nonleaf {root:true})WHERE NOT(:nonleaf {root:true})< - (r)RETURN r'。無論如何,萊昂的答案似乎是確切的。 – Mahesha999