|
| 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