Model One-to-Many Relationships with Embedded Documents
来源:互联网 发布:怎么看淘宝注册的时间 编辑:程序博客网 时间:2024/06/06 18:52
Model One-to-Many Relationships with Embedded Documents
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 embedded documents to describe relationships between connected data.
Pattern
Consider the following example that maps patron and multiple address relationships. The example illustrates the advantage of embedding over referencing if you need to view many data entities in context of another. In this one-to-many relationship between patron
and address
data, the patron
has multiple address
entities.
In the normalized data model, the address
documents contain a reference to the patron
document.
{ _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"}
If your application frequently retrieves the address
data with the name
information, then your application needs to issue multiple queries to resolve the references. A more optimal schema would be to embed the address
data entities in the patron
data, as in the following document:
{ _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" } ] }
With the embedded data model, your application can retrieve the complete patron information with one query.
- Model One-to-Many Relationships with Embedded Documents
- Model One-to-One Relationships with Embedded Documents
- Model One-to-Many Relationships with Document References
- How to define One-Many and One-One relationships
- Many to many relationships&Assignment
- use mongoose to update documents with model and Schema
- 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映射
- 健康生活习惯
- centos7 安装elk日志分析系统
- 大数据量下高并发同步的讲解(不看,保证你后悔)
- javascript ES6 Map 和Set结构总结
- 基数排序
- Model One-to-Many Relationships with Embedded Documents
- Apache、nginx 、lighttpd
- 搭建zimg 高可用图片服务器
- 关于XMLHttpRequest.open()设置提交方式 (POST,GET)
- AngularJS中控制器之间通信方法
- 变量及其传递
- 多行数据用json格式插入到一个字段中,然后再分别显示在不同表格增删改查
- RxJava2+MVP+Retrofit框架简单实现RecyclerView
- jquery实现input输入框输入值自动智能提示的实现