2014-01-28 75 views
0

我具有例如如下表結構 - 用戶,項目。導軌的MongoDB的has_many和embeds_many

用戶的has_many項目 項目的has_many照片

,但我想嵌入式項目和照片的某些基本內容到用戶表,這樣我可以減少對特定網頁的數據庫調用的次數。

下面是結構I紛紛拿出。

用戶表格

[ 
    { 
    "id": "LONG-MONGO-ID-HERE-USER-1", 
    "name": "Harsha MV", 
    "email": "[email protected]", 
    "gender": "male", 
    "telephone": "9986377561", 
    "is_pro": 1, 
    "projects": [ 
     { 
     "id": "LONG-MONGO-ID-HERE-PROJECT-1", 
     "name": "Nike", 
     "url": "http://nike.com", 
     "logo": "logo_nike.jpg", 
     "photos": [ 
      { 
      "title": "Some title for an Image", 
      "file": "project1_photo1.jpg" 
      }, 
      { 
      "title": "another title for an Image", 
      "file": "project1_photo2.jpg" 
      } 
     ] 
     }, 
     { 
     "id": "LONG-MONGO-ID-HERE-PROJECT-2", 
     "name": "BMW", 
     "url": "http://bmw.com", 
     "logo": "logo_bmw.jpg", 
     "photos": [ 
      { 
      "title": "Some title for an Image", 
      "file": "project2_photo1.jpg" 
      }, 
      { 
      "title": "another title for an Image", 
      "file": "project2_photo2.jpg" 
      } 
     ] 
     } 
    ] 
    }, 
    { 
    "id": "LONG-MONGO-ID-HERE-USER-2", 
    "name": "Pruthvi Gowda", 
    "email": "[email protected]", 
    "gender": "male", 
    "telephone": "9982318016", 
    "is_pro": 0, 
    "projects": [ 
     { 
     "id": "LONG-MONGO-ID-HERE-PROJECT-3", 
     "name": "Adidas", 
     "url": "http://adidas.com", 
     "logo": "logo_adidas.jpg", 
     "photos": [ 
      { 
      "title": "Some title for an Image", 
      "file": "project1_photo3.jpg" 
      }, 
      { 
      "title": "another title for an Image", 
      "file": "project1_photo4.jpg" 
      } 
     ] 
     }, 
     { 
     "id": "LONG-MONGO-ID-HERE-PROJECT-2", 
     "name": "BMW", 
     "url": "http://bmw.com", 
     "logo": "logo_bmw.jpg", 
     "photos": [ 
      { 
      "title": "Some title for an Image", 
      "file": "project2_photo1.jpg" 
      }, 
      { 
      "title": "another title for an Image", 
      "file": "project2_photo2.jpg" 
      } 
     ] 
     } 
    ] 
    } 
] 

表項目

[ 
    { 
     "id": "LONG-MONGO-ID-HERE-PROJECT-1", 
     "name": "Nike", 
     "url": "http://nike.com", 
     "logo": "logo_nike.jpg", 
     "about": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.", 
     "testimonial": "e middle of text. All the Lorem Ipsum generators on the Internet tend", 
     "photos": [ 
     { 
      "title": "Some title for an Image", 
      "description": "um is simply dummy text of the printing and t", 
      "file": "project1_photo1.jpg" 
     }, 
     { 
      "title": "another title for an Image", 
      "description": "text of the printing and t um is simply dummy", 
      "file": "project1_photo2.jpg" 
     } 
     ], 
     "user": { 
     "id": "LONG-MONGO-ID-HERE-USER-1", 
     "name": "Harsha MV", 
     "email": "[email protected]" 
     } 
    }, 
    { 
     "id": "LONG-MONGO-ID-HERE-PROJECT-2", 
     "name": "BMW", 
     "url": "http://bmw.com", 
     "logo": "logo_bmw.jpg", 
     "about": "It is a long established fact that a reader will be distracted by the", 
     "testimonial": "from sections 1.10.32 and 1.10.33 of de Finibus Bonorum et Malorum", 
     "photos": [ 
     { 
      "title": "Some title for an Image", 
      "description": "um is simply dummy text of the printing and t", 
      "file": "project2_photo1.jpg" 
     }, 
     { 
      "title": "another title for an Image", 
      "description": "text of the printing and t um is simply dummy", 
      "file": "project2_photo2.jpg" 
     } 
     ], 
     "user": { 
     "id": "LONG-MONGO-ID-HERE-USER-1", 
     "name": "Harsha MV", 
     "email": "[email protected]" 
     } 
    } 
    ] 

正如我理解MongoDB是所有有關數據的複製。這是設計數據庫結構的正確方法。

class User 
    include Mongoid::Document 

    field :first_name, type: String 
    field :last_name, type: String 
    field :company_name, type: String 


    embeds_many :projects, class_name: "Project" 
    has_many :projects, class_name: "Project" 

end 

我可以做一些像上面這樣我可以保存同一數據的兩個實例。 但正如你在嵌入式文件中看到的,我並沒有存儲來自項目的所有數據 - 我如何嚴格地將所有數據作爲嵌入式文檔添加,但作爲單獨的表存儲?

回答

2

這是不可能添加同型號嵌入式和引用集合。您可以通過引用用戶中的項目集合來將結構設置爲與前面提到的相同。

,創造假人模型說ImportantProject將被嵌入在用戶和同時使數據庫的變化就可以同步了在這個模型中的數據項目。

+0

你的意思是說,我只能在用戶表到項目引用直通一個假人模型? –

+0

不,我是說你可以創建用戶和項目之間的has_many關係,而是拯救計劃的其他數據,你可以創建一個被嵌入在用戶的虛擬模型,並可以保存你所需要的數據 – abhas

+0

我唯一關心的是從SQL到來背景。如果我想在表格中顯示項目的用戶和標題列表。我打電話給用戶。那麼我必須運行一個循環來再次獲取他所有項目的內容並構建一個對象,然後將其傳遞給視圖。而在mysql中,我們可以使用join並執行一次調用。這是我們如何在NoSQL中實現它的嗎? –