MongoDB 文档模型关系

文档模型关系

Model One-to-One Relationships with Embedded Documents

Overview

Data in MongoDB has a flexible schema. Collections do not enfoce document structure.

Pattern

In the normalized data model, the address document contains a reference to the patron document.

 
 
817474-20151010173013987-351662180.png
 
 

817474-20151010173015018-368326524.png

 

Model One-to-Many Relationships with Embedded Documents

Pattern

Consider the following example that map patron and multiple address relationships. In this one-to-many relationships between patron and address data, the patron has multiple address entities.

In the normalized data model, the address documents contain a reference to the parton document:

817474-20151010173015846-1572218705.png

If your application frequently retrieves the address data with name information, then your application needs to issue multiple queries to resolve the references. A more schema would to embed the address data entities in the patron data, as in the following  document:

817474-20151010173016518-812612128.png

Model one-to-Many Relationships with Document References

Pattern

Consider the following example that maps publisher and book relationships. The example illustrates the advantage of referencing over embedding to avoid repetition of publisher information.

Embeddding the publisher document inside the book document would lead to repetion of publisher data as the following documents show:

817474-20151010174539612-2022109964.png

To avoid repetion of the publisher data, use references and keep the publisher information in separate collection from the book collection.

When using references, the growth of the relationships determine where to store the reference. If the number of books per publisher is small with limited growth, storing the book references inside the publisher document may sometimes be useful. Otherwise, if the number of books per publihser is unbounded, this data model would lead to mutable, growing arrays, as in the following example:

817474-20151010174540565-1820590919.png

To avoid mutable, growing arrays, store the publisher reference inside the book document:

817474-20151010174541409-251656116.png

转载于:https://www.cnblogs.com/hotbaby/p/4867452.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值