2017-03-06 43 views
0

我有一個查找問題使用Morphia。 這裏是我的代碼:Mongorphb Morphia聚合查找匹配

class person{ 
private ObjectId id; 
private String name; 
} 

class book{ 
private ObjectId id; 
private String title; 
} 

class person_ownership{ 
private ObjectId id; 
private person p; 
private List<book> books; 
} 

List<book> BOOKs = new ArrayList<book>(); 

什麼,我想是通過搜索對個人徵收與藏書

這裏的ID過濾是我聚集管道

AggregationPipeline aggr = DS.createAggregation(person.class); 
aggr.lookup("person_ownership", "id", "p.id", "ownership"); 
aggr.unwind("ownership"); 

Query<person> q = DS.createQuery(person.class); 
q.disableValidation().criteria("ownership.books").in(BOOKs); 
aggr.match(q); 

Iterator<person> aggregate = aggr.aggregate(person.class, opts); 

我不得不q.disableValidation()它通過。但管道仍然沒有返回。

而我必須通過throu人查詢!

是甚至可能的?那裏沒有例外!

+0

這是可能的。你可以添加你試圖查詢的每個集合中的文檔嗎? – Veeram

+0

人:[{_ id:...,name:...}]; person_ownerships:[{_id:...,p:DBRef(「persons」,ObjectId(「...」)),books:[DBRef(「books」,ObjectId(「...」)),DBRef (「books」,ObjectId(「...」)),...]}]; books:[{_id:...,title:「...」}] –

+0

books:[{_id:...,title:「...」},...] 我們總是有把**放到一個新的集合? 如果是這樣的話,會出現什麼情況併發請求? 我試圖** aggr.unwind(「ownership.books」); **但仍然得到不正確的結果 –

回答

0

看起來像這樣是不可能的! Morphia不查找參考字段。 我不得不inste另一個字段引用人員ID爲聚合工作!