zoukankan      html  css  js  c++  java
  • 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.

     
    If the address data is frequently retrivevd with name information, then with referencing, your application needs to issue multiple queries to resolve the reference. The better data model would to be embed the address data in the patron data, as in the following document:

     

    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:

    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:

    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:

    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:

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

  • 相关阅读:
    双十一脱单就靠它:创维小湃蓝牙音箱体验评测
    专注产品真正价值:iWALK真无线蓝牙耳机体验评测
    美好的童年伙伴:360 智能儿童手表 P1体验评测
    前端也要学系列:设计模式之装饰者模式
    前端也要学系列:设计模式之策略模式
    你不知道的Javascript:有趣的setTimeout
    接受”不完美“:分布式事务学习总结
    如何进行团队技术分享
    又是一年寒冬时
    mybatis ~ 批量更新(sql循环)update foreach
  • 原文地址:https://www.cnblogs.com/hotbaby/p/4867452.html
Copyright © 2011-2022 走看看