MongoDB一对多存储

一个针对于顾客和其多个地址关系建模的一个例子

分别使用规范化(引用)和非规范化(内嵌)存储:

1、规范化存储:

(在一个规范化的数据模型中, address 文档包含一个对父文档 patron 的引用):

{
   _id: "joe",
   name: "Joe Bookreader"
}

{
   patron_id: "joe",
   street: "123 Fake Street",
   city: "Faketon",
   state: "MA",
   zip: "12345"
}

{
   patron_id: "joe",
   street: "1 Some Other Street",
   city: "Boston",
   state: "MA",
   zip: "12345"
}

2、非规范化存储:

(把 address 数据直接内嵌到 patron 文档里面)

{
   _id: "joe",
   name: "Joe Bookreader",
   addresses: [
                {
                  street: "123 Fake Street",
                  city: "Faketon",
                  state: "MA",
                  zip: "12345"
                },
                {
                  street: "1 Some Other Street",
                  city: "Boston",
                  state: "MA",
                  zip: "12345"
                }
              ]
 }
相关文章
相关标签/搜索