2012-03-03 37 views
3

我正在構建電子商務網站,並決定試用MongoDB。我的目標是實現充分的靈活性,因此最終我不會被束縛於銷售特定的產品,這受限於系統最初的組裝方式。MongoDB電子商務產品文檔設計

因此,靈活性的目標,我必須能夠創建基於屬性的產品。 FX。顏色,製造,速度等。所有屬性必須是可選的。用戶可以創建新的屬性,有些是默認的系統屬性(不可刪除)。根據屬性的配置,它將與「基礎」 - 或可配置產品分層。

  • 在目錄中,我想分割具有顏色屬性的產品,以便每種顏色以單個產品的形式呈現。我可以使用我當前的文檔設計在MongoDB中實現嗎?
  • 我得出的結論是,在從MongoDB中選擇產品時,在呈現之前,我必須「填充」文檔或進行大量數據映射。我錯了嗎?
  • 考慮到某些產品有選項(顏色,尺寸)等,沒有哪種做庫存管理的最好和最有效的方法?

正如在我的例子中看到的,我有屬性下存儲的普通屬性,以及文檔選項下的「required」屬性。

爲了讓事情變得簡單,我的產品文檔可以以某種方式進行改進嗎?我擔心自己要麼過度執行某些任務,要麼因爲通過使用關係數據庫而發現自己的想法「受損」。

問候

(
      [type] => Product 
      [sku] => elin/4191 
      [name] => Array 
       (
        [da] => Sko - Elin 
        [en] => Shoes - Elin 
       ) 

      [url_key] => Array 
       (
        [da] => sko-elin 
        [en] => 1-744 
       ) 

      [categories] => Array 
       (
       ) 

      [shops] => Array 
       (
        [0] => 1 
       ) 

      [images] => Array 
       (
        [0] => test.jpg 
        [1] => test1.jpg 
       ) 

      [options] => Array 
       (
        [0] => Array 
         (
          [color] => Array 
           (
            [da] => Sort 
            [en] => Black 
           ) 

          [size] => Array 
           (
            [da] => Lille 
            [en] => Small 
           ) 

          [shipping] => Array 
           (
            [weight] => 0 
            [width] => 0 
            [height] => 0 
            [depth] => 0 
           ) 

          [pricing] => Array 
           (
            [price] => 899 
            [retail] => 0 
            [cost] => 333 
            [vat] => 25 
            [special] => Array 
             (
              [price] => 0 
              [from] => new Date() 
              [to] => new Date() 
              [pct_savings] => 100 
              [savings] => 0 
             ) 

           ) 

         ) 

        [1] => Array 
         (
          [color] => Array 
           (
            [da] => Sort 
            [en] => Black 
           ) 

          [size] => Array 
           (
            [da] => Medium 
            [en] => Medium 
           ) 

          [shipping] => Array 
           (
            [weight] => 0 
            [width] => 0 
            [height] => 0 
            [depth] => 0 
           ) 

          [pricing] => Array 
           (
            [price] => 899 
            [retail] => 0 
            [cost] => 333 
            [vat] => 25 
            [special] => Array 
             (
              [price] => 0 
              [from] => new Date() 
              [to] => new Date() 
              [pct_savings] => 100 
              [savings] => 0 
             ) 

           ) 

         ) 

        [2] => Array 
         (
          [color] => Array 
           (
            [da] => Orange 
            [en] => Orange 
           ) 

          [size] => Array 
           (
            [da] => Lille 
            [en] => Small 
           ) 

          [shipping] => Array 
           (
            [weight] => 0 
            [width] => 0 
            [height] => 0 
            [depth] => 0 
           ) 

          [pricing] => Array 
           (
            [price] => 899 
            [retail] => 0 
            [cost] => 333 
            [vat] => 25 
            [special] => Array 
             (
              [price] => 0 
              [from] => new Date() 
              [to] => new Date() 
              [pct_savings] => 100 
              [savings] => 0 
             ) 

           ) 

         ) 

        [3] => Array 
         (
          [color] => Array 
           (
            [da] => Orange 
            [en] => Orange 
           ) 

          [size] => Array 
           (
            [da] => Medium 
            [en] => Medium 
           ) 

          [shipping] => Array 
           (
            [weight] => 0 
            [width] => 0 
            [height] => 0 
            [depth] => 0 
           ) 

          [pricing] => Array 
           (
            [price] => 899 
            [retail] => 0 
            [cost] => 333 
            [vat] => 25 
            [special] => Array 
             (
              [price] => 0 
              [from] => new Date() 
              [to] => new Date() 
              [pct_savings] => 100 
              [savings] => 0 
             ) 

           ) 

         ) 

       ) 

      [attributes] => Array 
       (
        [designer] => Array 
         (
          [name] => Array 
           (
            [da] => Manufacturer 
            [en] => Manufacturer 
           ) 

          [type] => text 
          [visible] => 1 
          [required] => false 
          [value] => Array 
           (
            [da] => FunnyShirts 
            [en] => FunnyShirts 
           ) 

         ) 

       ) 

     ) 

回答

1

我覺得你的設計做工精細,雖然我不認爲有必要要求和可選的屬性分成單獨的子文檔。

我不知道PHP,但對待每一種顏色作爲一個單獨的產品,當你顯示,你可以這樣做:

product = db.products.findOne({sku: "..."}) 
for color in product.colors: 
    # render the product with the color 

沒有必要爲「溢出」您的文檔 - 我相信你的意思是爲每個可能的屬性存儲空值?您的應用程序代碼應該簡單地檢查文檔中是否存在可選值,並基於此進行渲染或業務邏輯決策。 MongoDB的優勢在於它的靈活性。並非所有文件必須相同。您的應用程序代碼應該寫成處理沒有所有可能字段的文檔。