MongoDB数据关系建模


MongoDB中的数据是非常灵活的,集合中也不强制文档要采用统一的结构。但是认真考虑数据模型依然是非常重要的,因为这会影响到应用程序性能和数据库的能力。本文讲述了MongoDB中常见的一对一、一对多关系模型如如何建模。

CentOS编译安装MongoDB

CentOS 编译安装 MongoDB与mongoDB的php扩展

CentOS 6 使用 yum 安装MongoDB及服务器端配置

Ubuntu 13.04下安装MongoDB2.4.3

MongoDB入门必读(概念与实战并重)

《MongoDB 权威指南》(MongoDB: The Definitive Guide)英文文字版[PDF]

 (1)一对一嵌入式文档模型(Embedded Document Model)

假设赞助商和住址是一种一对一关系,赞助商只有一处住址。赞助商可以看成是住址的属性或字段,住址也可以看成是赞助商的一个属性或字段。在类似于这种关系中,使用嵌入式数据模型(Embedded)的好处就是在一次查询中就能得到想要的全部数据,而引用性模型(References)则需要多次查询才能得到想要的数据(姓名和住址)。

赞助商和住址之间的关系(References)

{

  _id: "joe",

  name: "Joe Bookreader"

}

{

  patron_id: "joe",

  street: "123 Fake Street",

  city: "Faketon",

  state: "MA",

  zip: "12345"

}


赞助商和住址之间的关系(Embeded)

{

  _id: "joe",

  name: "Joe Bookreader",

  address: {

              street: "123 Fake Street",

              city: "Faketon",

              state: "MA",

              zip: "12345"

            }

}


(2)一对多嵌入式文档模型(Embedded Document Model)

假设赞助商和住址是一种一对多关系,赞助商有多处住址,可以使用引用模型将赞助商当做住址的属性,可以使用嵌入模型将住址当成赞助商的属性。这样的场景适合使用嵌入式模型,一来只有一次查询就能得到想要的所有数据。二来,在一个上下文中就能看到数据数据,结构比较简单。

赞助商和住址之间的关系(References)


{

  _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"

}
 


    赞助商和住址之间的关系(Embeded)

 

 

{

  _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"

                }

              ]

 }
 

(三)一对多引用型文档模型(References Document Model)

 图书出版商和图书之间是一种一对多关系,一个出版本可以初版多本图书,可以一本图书只能由一个出版商发行。在这种情形下,如果我们仍使用嵌入式数据模型,可能会导致数据重复,见下图:

{

  title: "MongoDB: The Definitive Guide",

  author: [ "Kristina Chodorow", "Mike Dirolf" ],

  published_date: ISODate("2010-09-24"),

  pages: 216,

  language: "English",

  publisher: {

              name: "O'Reilly Media",

              founded: 1980,

              location: "CA"

            }

}

{

  title: "50 Tips and Tricks for MongoDB Developer",

  author: "Kristina Chodorow",

  published_date: ISODate("2011-05-06"),

  pages: 68,

  language: "English",

  publisher: {

              name: "O'Reilly Media",

              founded: 1980,

              location: "CA"

            }

}

为了避免出现数据重复,最好的方法是使用引用型数据模型,将图书出版商和初版图书分别保存在不同的集合中。

使用引用模型时,引用关系存储在哪一方是由关系之间的数据量决定的。如果出版商的图书增长的非常缓慢,也可以说是每个出版商出版的图书数量有限,可以将关系存储在出版商这边。如下所示:

{

  name: "O'Reilly Media",

  founded: 1980,

  location: "CA",

  books: [12346789, 234567890, ...]

}

{

    _id: 123456789,

    title: "MongoDB: The Definitive Guide",

    author: [ "Kristina Chodorow", "Mike Dirolf" ],

    published_date: ISODate("2010-09-24"),

    pages: 216,

    language: "English"

}

{

  _id: 234567890,

  title: "50 Tips and Tricks for MongoDB Developer",

  author: "Kristina Chodorow",

  published_date: ISODate("2011-05-06"),

  pages: 68,

  language: "English"

}

但如果出版商出版的图书数量非常多,这种模型会导致数据模型发生变化,尤其是增长的数组。这时,最好将引用关系存储在图书一方,如下图:

{

  _id: "oreilly",

  name: "O'Reilly Media",

  founded: 1980,

  location: "CA"

}

{

  _id: 123456789,

  title: "MongoDB: The Definitive Guide",

  author: [ "Kristina Chodorow", "Mike Dirolf" ],

  published_date: ISODate("2010-09-24"),

  pages: 216,

  language: "English",

  publisher_id: "oreilly"

}

{

  _id: 234567890,

  title: "50 Tips and Tricks for MongoDB Developer",

  author: "Kristina Chodorow",

  published_date: ISODate("2011-05-06"),

  pages: 68,

  language: "English",

  publisher_id: "oreilly"

}

MongoDB 的详细介绍:请点这里
MongoDB 的下载地址:请点这里

本文永久更新链接地址:

相关内容

    暂无相关文章