2013-07-23 33 views
0

我在Neo4J中運行以下查詢,並且出現錯誤。簡而言之,我要求數據庫向我顯示所有帶有「user」標籤的節點,如果它有關係,則顯示它所連接的節點的關係類型和標籤類型。如何爲Neo4J中不存在的標籤獲取null?

START n=node(*) MATCH (n:user)-[r?]-(m) WHERE HAS (n.name) RETURN n, labels(n), type(r), labels(m), m.name; 

發現類型爲user的節點沒有與其相關的節點時發生錯誤。它似乎正在炸燬標籤(m)部分。我期望如果節點不存在,我會得到一個NULL返回而不是一個錯誤。這是type()函數的工作原理。如果找不到關係,則類型()返回NULL。

這裏是我的結果看起來像如果我忽略的標籤(M)的一部分......

==>+-----------------------------------------------------------------------------------------------------------------------------------+ 
==> | n                     | labels(n) | type(r)  | m.name   | 
==> +-----------------------------------------------------------------------------------------------------------------------------------+ 
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"} | ["user"] | "hasContext" | "vinniecontext" | 
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"} | ["user"] | "hasContext" | "vinniecontext" | 
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"} | ["user"] | "hasContext" | "vinniecontext" | 
==> | Node[2558]{lastName:"b",mobilePhone:"b",status:"b",email:"b",name:"b",firstName:"b"} | ["user"] | <null>  | <null>   | 
==> | Node[2559]{lastName:"c",mobilePhone:"c",status:"c",email:"c",name:"c",firstName:"c"} | ["user"] | <null>  | <null>   | 
==> | Node[2560]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"} | ["user"] | <null>  | <null>   | 
==> | Node[2561]{lastName:"b",mobilePhone:"b",status:"b",email:"b",name:"",firstName:"b"} | ["user"] | <null>  | <null>   | 
==> +-----------------------------------------------------------------------------------------------------------------------------------+ 

我怎樣才能得到我想要的結果呢?在沒有連接節點的情況下,我基本上需要一個NULL值,否則給我連接節點的標籤。

在此先感謝!

回答

2

好吧,一位同事幫助我解決了這個問題(在我把頭靠在桌子上呆了一天半之後)。我通過使用UNION ALL得到了我想要的結果。我必須做兩個不同的查詢;一個關係爲空,另一個關係不爲空。

START n=node(*) MATCH (n:user)-[r?]-(m) WHERE r IS NOT NULL RETURN n, labels(n), type(r), labels(m) as i, m.name as a 
UNION ALL 
START n=node(*) MATCH (n:user)-[r?]-(m) WHERE r IS NULL RETURN n, labels(n), type(r), null as i, null as a; 

而這裏的結果......

==> +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 
==> | n                                    | labels(n) | type(r)  | i   | a    | 
==> +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"}               | ["user"] | "hasContext" | ["context"] | "vinniecontext" | 
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"}               | ["user"] | "hasContext" | ["context"] | "vinniecontext" | 
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"}               | ["user"] | "hasContext" | ["context"] | "vinniecontext" | 
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"}               | ["user"] | "hasContext" | ["context"] | "vinniecontext" | 
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"}               | ["user"] | "hasContext" | ["context"] | "vinniecontext" | 
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"}               | ["user"] | "hasContext" | ["context"] | "vinniecontext" | 
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"}               | ["user"] | "hasContext" | ["context"] | "vinniecontext" | 
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"}               | ["user"] | "hasContext" | ["context"] | "vinniecontext" | 
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"}               | ["user"] | "hasContext" | ["context"] | "vinniecontext" | 
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"}               | ["user"] | "hasContext" | ["context"] | "vinniecontext" | 
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"}               | ["user"] | "hasContext" | ["context"] | "vinniecontext" | 
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"}               | ["user"] | "hasContext" | ["context"] | "vinniecontext" | 
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"}               | ["user"] | "hasContext" | ["context"] | "vinniecontext" | 
==> | Node[2557]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"}               | ["user"] | "hasContext" | ["context"] | "vinniecontext" | 
==> | Node[2558]{lastName:"b",mobilePhone:"b",status:"b",email:"b",name:"b",firstName:"b"}               | ["user"] | <null>  | <null>  | <null>   | 
==> | Node[2559]{lastName:"c",mobilePhone:"c",status:"c",email:"c",name:"c",firstName:"c"}               | ["user"] | <null>  | <null>  | <null>   | 
==> | Node[2560]{lastName:"a",mobilePhone:"a",status:"a",email:"a",name:"a",firstName:"a"}               | ["user"] | <null>  | <null>  | <null>   | 
==> | Node[2561]{lastName:"b",mobilePhone:"b",status:"b",email:"b",name:"",firstName:"b"}                | ["user"] | <null>  | <null>  | <null>   | 
==> | Node[7638]{lastName:"afdasdfasdf",mobilePhone:"asdfa",status:"asdfasdf",email:"asdfasdfasdf",name:"asdfasdfa",firstName:"asdfasdfas"}   | ["user"] | <null>  | <null>  | <null>   | 
==> | Node[7639]{lastName:"asddsafsda",mobilePhone:"asdfasdfdsa",status:"sadfsadfsda",email:"sadfasdfas",name:"sdafasdfsadf",firstName:"sadfsadfsad"} | ["user"] | <null>  | <null>  | <null>   | 
==> +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 

我希望這個解決方案節省了別人一些痛苦。

相關問題