2015-01-12 100 views
9

我想引用嵌套對象存在於另一個集合對象中,在我的Event對象中,以便當我得到Event的用戶比事件的場所應檢索與它,我是新的MongoDB春天可以幫助你做到這一點。如何將嵌套對象引用到其他集合Mongodb

class User{ 
private String name; 
private Venue venue; 

//Getter and Setter 
} 

class Event { 
@DBRef 
private Venue venue; 

//Getter and Setter 
} 
+1

你能提供更多關於你的問題的細節嗎?也許你可以重讀你的問題來檢查你是否把每一個字都放在它的位置。我很難理解。我可以看到類User和類Event。他們有什麼關係? – innoSPG

+0

我偶然發現了同樣的問題 - 使用自己的ObjectId嵌套對象(Venue),然後從另一個對象(Event)引用DB。基本上歸結爲引用嵌套對象而不是頂級集合中的一個。 –

回答

2

如果每個用戶都有一個Venue,則可以在Event中引用User而不是Venue。如果用戶可能有多個場地,則最好讓場地在用戶和事件中分別收集和參考。

0

試試這個。

class Venue{ 
    //Getter and Setter 
} 

class User{ 
    private String name; 
    @DBRef 
    private Venue venue; 

    //Getter and Setter 
} 

class Event { 
    @DBRef 
    private Venue venue; 

    //Getter and Setter 
} 
相關問題