2015-08-21 40 views
0

考慮以下集合:如何實現的SQL加盟等效MongoDB的[ReactiveMongo]

// Users 
{ "_id": ObjectId("1"), "username": "user1", "role": "developer" } 
{ "_id": ObjectId("2"), "username": "user2", "role": "projectManager" } 
{ "_id": ObjectId("3"), "username": "user3", "role": "testManager" } 
{ "_id": ObjectId("4"), "username": "user4", "role": "developer" } 

// Projects 
{ "_id": ObjectId("1"), "title": "Monitoring System", ... } 
{ "_id": ObjectId("2"), "title": "Search Engine", ... } 
{ "_id": ObjectId("3"), "title": "IDE for Scala", ... } 

// Contributions 
{ "_id": ObjectId("1"), "projectId": ObjectId("1"), "userId": ObjectId("1") } 
{ "_id": ObjectId("2"), "projectId": ObjectId("1"), "userId": ObjectId("2") } 
{ "_id": ObjectId("3"), "projectId": ObjectId("1"), "userId": ObjectId("3") } 
{ "_id": ObjectId("4"), "projectId": ObjectId("2"), "userId": ObjectId("1") } 

使用ReactiveMongo,我怎麼得到所有誰貢獻的用戶項目1個?我需要整個文檔...不僅僅是用戶ID。

+0

我看到了這個答案......但這不是我要找的。我認爲有更好的方法,如聚合框架,但我無法找到一個符合我的需求的例子。 – j3d

+0

這是因爲它不存在。 :) MongoDB查詢只能跨越一個集合。 – JohnnyHK

回答

0

有MongoDB中沒有加入這種情況下我想使嵌入您的數據,並使用真實的鏈接,例如:

//用戶

{_id : id, username: name, role: role, Contributions : [projectId1, projectId2, ..] } 

這樣,你可以做一個簡單的查找來獲取所有用戶文檔在貢獻數組中具有特定項目標識。

將豐富的數據嵌入到集合中使Mongodb具有如此高性能和靈活性。

+0

不......這不起作用,因爲在我的真實情況下,一個項目可能有成千上萬的貢獻者...... – j3d