@@ -10,45 +10,46 @@ Data Modeling Introduction
10
10
:depth: 1
11
11
:class: singlecol
12
12
13
- Data in MongoDB has a *flexible schema*. Unlike SQL databases, where
14
- you must determine and declare a table's schema before inserting data,
15
- MongoDB's :term:`collections <collection>` do not enforce
16
- :term:`document` structure. This flexibility facilitates the mapping of
17
- documents to an entity or an object. Each document can match the data
18
- fields of the represented entity, even if the data has substantial
19
- variation. In practice, however, the documents in a collection share a
20
- similar structure.
21
-
22
13
The key challenge in data modeling is balancing the needs of the
23
14
application, the performance characteristics of the database engine, and
24
15
the data retrieval patterns. When designing data models, always
25
16
consider the application usage of the data (i.e. queries, updates, and
26
17
processing of the data) as well as the inherent structure of the data
27
18
itself.
28
19
29
- Document Structure
30
- ------------------
20
+ Flexible Schema
21
+ ---------------
31
22
32
- .. start-primer-excerpt
23
+ Unlike SQL databases, where you must determine and declare a table's
24
+ schema before inserting data, MongoDB's :term:`collections
25
+ <collection>`, by default, does not require its :doc:`documents
26
+ </core/document>` to have the same schema. That is:
33
27
34
- The key decision in designing data models for MongoDB applications
35
- revolves around the structure of documents and how the application
36
- represents relationships between data. There are two tools that allow
37
- applications to represent these relationships: *references* and
38
- *embedded documents*.
28
+ - The documents in a single collection do not need to have the same set
29
+ of fields and the data type for a field can differ across documents
30
+ within a collection.
39
31
40
- References
41
- ~~~~~~~~~~
32
+ - To change the structure of the documents in a collection, such as add
33
+ new fields, remove existing fields, or change the field values to a
34
+ new type, update the documents to the new structure.
42
35
43
- References store the relationships between data by including
44
- links or *references* from one document to another. Applications can
45
- resolve these :doc:`references </reference/database-references>` to
46
- access the related data. Broadly, these are *normalized* data models .
36
+ This flexibility facilitates the mapping of documents to an entity or
37
+ an object. Each document can match the data fields of the represented
38
+ entity, even if the document has substantial variation from other
39
+ documents in the collection .
47
40
48
- .. include:: /images/data-model-normalized.rst
41
+ In practice, however, the documents in a collection share a similar
42
+ structure, and you can enforce :doc:`document validation rules
43
+ </core/schema-validation>` for a collection during update and insert
44
+ operations. See :doc:`/core/schema-validation` for details.
49
45
50
- See :ref:`data-modeling-referencing` for the strengths and weaknesses of
51
- using references.
46
+ Document Structure
47
+ ------------------
48
+
49
+ The key decision in designing data models for MongoDB applications
50
+ revolves around the structure of documents and how the application
51
+ represents relationships between data. MongoDB allows related data to
52
+ be embedded within a single document.
52
53
53
54
Embedded Data
54
55
~~~~~~~~~~~~~
@@ -62,42 +63,46 @@ database operation.
62
63
63
64
.. include:: /images/data-model-denormalized.rst
64
65
66
+ For many use cases in MongoDB, the denormalized data model is optimal.
67
+
65
68
See :ref:`data-modeling-embedding` for the strengths and weaknesses of
66
69
embedding documents.
67
70
68
- .. end-primer-excerpt
71
+ References
72
+ ~~~~~~~~~~
73
+
74
+ References store the relationships between data by including
75
+ links or *references* from one document to another. Applications can
76
+ resolve these :doc:`references </reference/database-references>` to
77
+ access the related data. Broadly, these are *normalized* data models.
78
+
79
+ .. include:: /images/data-model-normalized.rst
80
+
81
+ See :ref:`data-modeling-referencing` for the strengths and weaknesses of
82
+ using references.
69
83
70
84
Atomicity of Write Operations
71
85
-----------------------------
72
86
73
- In MongoDB, write operations are atomic at the :term:`document` level,
74
- and no single write operation can atomically affect more than one
75
- document or more than one collection. A denormalized data model with
76
- embedded data combines all related data for a represented entity in a
77
- single document. This facilitates atomic write operations since a
78
- single write operation can insert or update the data for an entity.
79
- Normalizing the data would split the data across multiple collections
80
- and would require multiple write operations that are not atomic
81
- collectively.
82
-
83
- However, schemas that facilitate atomic writes may limit ways that
84
- applications can use the data or may limit ways to modify applications.
85
- The :ref:`Atomicity Considerations <data-model-atomicity>`
86
- documentation describes the challenge of designing a schema that
87
- balances flexibility and atomicity.
88
-
89
- Document Growth
90
- ---------------
87
+ Single Document Atomicity
88
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
91
89
92
- Some updates, such as pushing elements to an array or adding new
93
- fields, increase a :term:`document's <document>` size.
90
+ In MongoDB, a write operation is atomic on the level of a single
91
+ document, even if the operation modifies multiple embedded documents
92
+ *within* a single document.
93
+
94
+ A denormalized data model with embedded data combines all related data
95
+ in a single document instead of normalizing across multiple documents
96
+ and collections. This data model facilitates atomic operations.
97
+
98
+ .. include:: /includes/extracts/concurrent-operations-multi-document-writes.rst
94
99
95
- For the MMAPv1 storage engine, if the document size exceeds the
96
- allocated space for that document, MongoDB relocates the document on
97
- disk. When using the MMAPv1 storage engine, growth consideration can
98
- affect the decision to normalize or denormalize data. See
99
- :ref:`Document Growth Considerations <data-model-document-growth>` for
100
- more about planning for and managing document growth for MMAPv1.
100
+ Multi-Document Transactions
101
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
102
+
103
+ .. include:: /includes/extracts/transactions-intro.rst
104
+
105
+ .. seealso:: :ref:`Atomicity Considerations <data-model-atomicity>`
101
106
102
107
Data Use and Performance
103
108
------------------------
@@ -110,3 +115,18 @@ adding indexes to support common queries can improve performance.
110
115
111
116
See :doc:`/core/data-model-operations` for more information on these
112
117
and other operational considerations that affect data model designs.
118
+
119
+ Document Growth and MMAPv1
120
+ --------------------------
121
+
122
+ Some updates, such as pushing elements to an array or adding new
123
+ fields, increase a :term:`document's <document>` size.
124
+
125
+ For the :doc:`deprecated MMAPv1 </core/mmapv1>` storage engine, if the
126
+ document size exceeds the allocated space for that document, MongoDB
127
+ relocates the document on disk. When using the deprecated MMAPv1
128
+ storage engine, growth consideration can affect the decision to
129
+ normalize or denormalize data. See :ref:`Document Growth Considerations
130
+ <data-model-document-growth>` for more about planning for and managing
131
+ document growth for MMAPv1.
132
+
0 commit comments