Skip to content

Commit ac3f4e8

Browse files
DOCSP-31880 Many-to-Many Embedded Docs (#5138) (#7055)
* DOCSP-31880 many to many * DOCSP-31880 fix example * DOCSP-31880 nit push * DOCSP-31880 add page to toc * DOCSP-31880 nit fix * DOCSP-31880 update example * DOCSP-31880 test build * DOCSP-31880 internal feedback * DOCSP-31880 new staging link * DOCSP-31880 max feedback * DOCSP-31880 remove quotes
1 parent 6e10941 commit ac3f4e8

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

source/applications/data-models-relationships.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ Model Relationships Between Documents
2525
<data-modeling-referencing>` to describe one-to-many relationships
2626
between documents.
2727

28+
:doc:`/tutorial/model-embedded-many-to-many-relationships-between-documents`
29+
Presents a data model that uses :ref:`embedded documents
30+
<data-modeling-embedding>` to describe many-to-many relationships
31+
between connected data.
2832

2933
.. toctree::
3034
:titlesonly:
@@ -33,3 +37,4 @@ Model Relationships Between Documents
3337
/tutorial/model-embedded-one-to-one-relationships-between-documents
3438
/tutorial/model-embedded-one-to-many-relationships-between-documents
3539
/tutorial/model-referenced-one-to-many-relationships-between-documents
40+
/tutorial/model-embedded-many-to-many-relationships-between-documents

source/applications/data-models.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ patterns and common schema design considerations:
3838
<data-modeling-embedding>` to describe one-to-many
3939
relationships between connected data.
4040

41+
:doc:`/tutorial/model-embedded-many-to-many-relationships-between-documents`
42+
Presents a data model that uses :ref:`embedded documents
43+
<data-modeling-embedding>` to describe many-to-many relationships
44+
between connected data.
45+
4146
:doc:`/tutorial/model-referenced-one-to-many-relationships-between-documents`
4247
Presents a data model that uses :ref:`references
4348
<data-modeling-referencing>` to describe one-to-many

source/tutorial.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ Data Modeling Patterns
134134

135135
- :doc:`/tutorial/model-embedded-one-to-one-relationships-between-documents`
136136
- :doc:`/tutorial/model-embedded-one-to-many-relationships-between-documents`
137+
- :doc:`/tutorial/model-embedded-many-to-many-relationships-between-documents`
137138
- :doc:`/tutorial/model-referenced-one-to-many-relationships-between-documents`
138139
- :doc:`/tutorial/model-data-for-atomic-operations`
139140
- :doc:`/tutorial/model-tree-structures-with-parent-references`
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
.. _data-modeling-example-many-to-many:
2+
3+
========================================================
4+
Model Many-to-Many Relationships with Embedded Documents
5+
========================================================
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 2
13+
:class: singlecol
14+
15+
Create a data model that uses :ref:`embedded
16+
<data-modeling-embedding>` documents to describe a many-to-many relationship
17+
between connected data. Embedding connected data in a single document can
18+
reduce the number of read operations required to obtain data. In general,
19+
structure your schema so your application receives all of its required
20+
information in a single read operation. For example, you can use the embedded
21+
many-to-many model to describe the following relationships:
22+
23+
- Students to classes
24+
- Actors to movies
25+
- Doctors to patients
26+
27+
About this Task
28+
---------------
29+
30+
The following example schema contains information regarding ``book one``
31+
and ``book two`` and their authors. You can represent the relationship
32+
differently based on whether you anticipate application users querying by
33+
book or by author.
34+
35+
If you expect more users to query by book than by author, the example schema
36+
is an effective choice. However, if you expect more queries by author, make
37+
the author the top-level information and place the author's books in an embedded
38+
field.
39+
40+
Example
41+
-------
42+
43+
You can use a many-to-many relationship to describe books and authors. A book
44+
can have multiple authors, and an author can write multiple books.
45+
46+
Embedded Document Pattern
47+
~~~~~~~~~~~~~~~~~~~~~~~~~
48+
49+
The application needs to display information for the book and author
50+
objects on a single page. To allow your application to retrieve all
51+
necessary information with a single query, embed author information
52+
inside of the corresponding book document:
53+
54+
.. code-block:: javascript
55+
56+
{
57+
_id: "book001",
58+
title: "Cell Biology",
59+
authors: [
60+
{
61+
author_id: "author124",
62+
name: "Ellie Smith"
63+
},
64+
{
65+
author_id: "author381",
66+
name: "John Palmer"
67+
}
68+
]
69+
}
70+
71+
{
72+
_id: "book002",
73+
title: "Organic Chemistry",
74+
authors: [
75+
{
76+
author_id: "author290",
77+
name: "Jane James"
78+
},
79+
{
80+
author_id: "author381",
81+
name: "John Palmer"
82+
}
83+
]
84+
}
85+
86+
Learn More
87+
----------
88+
89+
- :ref:`data-modeling-example-one-to-one`
90+
91+
- :ref:`data-modeling-example-one-to-many`

0 commit comments

Comments
 (0)