Model One-to-Many Relationships with Document References
来源:互联网 发布:java类可以带参数吗 编辑:程序博客网 时间:2024/06/07 23:13
Model One-to-Many Relationships with Document References
On this page
- Overview
- Pattern
Overview
Data in MongoDB has a flexible schema. Collections do not enforce document structure. Decisions that affect how you model data can affect application performance and database capacity. See Data Modeling Conceptsfor a full high level overview of data modeling in MongoDB.
This document describes a data model that uses references between documents to describe relationships between connected data.
Pattern
Consider the following example that maps publisher and book relationships. The example illustrates the advantage of referencing over embedding to avoid repetition of the publisher information.
Embedding the publisher document inside the book document would lead to repetition of the publisher data, as the following documents show:
{ 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" }}
To avoid repetition of the publisher data, use references and keep the publisher information in a 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 reference inside the publisher document may sometimes be useful. Otherwise, if the number of books per publisher is unbounded, this data model would lead to mutable, growing arrays, as in the following example:
{ name: "O'Reilly Media", founded: 1980, location: "CA", books: [123456789, 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"}
To avoid mutable, growing arrays, store the publisher reference inside the book document:
{ _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"}
- Model One-to-Many Relationships with Document References
- Model One-to-Many Relationships with Embedded Documents
- Model One-to-One Relationships with Embedded Documents
- How to define One-Many and One-One relationships
- Many to many relationships&Assignment
- Mutable Access of To-Many Relationships
- <many to one>和<one to many>
- <many-to-one>和<one-to-many>
- hibernate many-to-one // one-to-many
- djang Extra fields on many-to-many relationships
- nhibernate one to one,one to many
- one to many
- Many-to-one query
- Many-to-One映射
- hibernate many-to-one
- many-to-one
- Many-to-One映射
- hibernate one to many
- 线程基础练习测试
- Android-- FragmentStatePagerAdapter
- 下一代前端打包工具-Parcel介绍
- 分页插件
- Nginx配置文件详细说明
- Model One-to-Many Relationships with Document References
- mysql实现ROW_NUMBER() over (PARTITION BY xx ORDER BY ** DESC)
- 最全面免费下载的Altium designer 3D元件封装库模型效果图库,可以直接在工程中应用
- 使用oracel 出现 java.lang.IllegalArgumentException: argument type mismatch解决
- [大数据-hadoop基础]window下编译hadoop2.7.4-eclipse mar2插件
- ffmpeg心得体会
- Android 沉浸状态
- ORACLE 12C 默认启动PDB
- 安卓系统app可用内存分析