@@ -4,10 +4,26 @@ Documents
4
4
5
5
.. default-domain:: mongodb
6
6
7
- The document structure in MongoDB refer to the data structure of the
8
- records stored in :term:`collections <collection>`, the query criteria,
9
- the update actions, and other arguments to MongoDB methods and
10
- operators.
7
+ The document structure in MongoDB refer to the data structure of:
8
+
9
+ - the :ref:`records <documents-records>` stored in :term:`collections
10
+ <collection>`
11
+
12
+ - the :ref:`query selectors <documents-query-selectors>` that determine
13
+ which records to select for read, update, and delete operations
14
+
15
+ - the :ref:`update actions <documents-update-actions>` that specifies
16
+ the particular field updates to perform during an update operation
17
+
18
+ - the :ref:`index <documents-index>` on a collection
19
+
20
+ - the various other arguments to MongoDB methods and operators, such as:
21
+
22
+ - :ref:`sort order <documents-sort-order>` for the :method:`sort()
23
+ <cursor.sort()>` method.
24
+
25
+ Structure
26
+ ---------
11
27
12
28
The document structure in MongoDB are :term:`BSON` objects with support
13
29
for the full range of :term:`BSON types`; however, conceptually,
@@ -26,15 +42,24 @@ structure:
26
42
27
43
Having support for the full range of :term:`BSON types`, MongoDB
28
44
documents may contain ``field:value`` pairs where the value can be
29
- another document, an array, an array of documents as well as the basic
30
- types such as ``Double``, ``String``, or ``Date``.
45
+ another document, an array, an array of documents as well
46
+ as the basic types such as ``Double``, ``String``, or ``Date``.
31
47
32
- .. include:: /includes/warning-document-field-name-restrictions.rst
48
+ .. _documents-records:
33
49
34
- Consider the following examples of MongoDB documents:
50
+ Document as records
51
+ -------------------
35
52
36
- - The following document specifies a record in a collection. Documents
37
- in a collection contain a unique ``_id`` field:
53
+ For documents that specify records to be stored in a collection, the
54
+ following restrictions apply:
55
+
56
+ - .. include:: /includes/fact-document-max-size.rst
57
+
58
+ - .. include:: /includes/fact-document-field-name-restrictions.rst
59
+
60
+ .. include:: /includes/note-insert-id-field.rst
61
+
62
+ The following document specifies a record in a collection:
38
63
39
64
.. code-block:: javascript
40
65
@@ -54,89 +79,164 @@ Consider the following examples of MongoDB documents:
54
79
]
55
80
}
56
81
57
- .. include:: /includes/note-insert-id-field.rst
58
-
59
- - The following documents specify the query criteria:
82
+ The document contains the following fields:
83
+
84
+ - field ``_id`` whose value must be unique
60
85
61
- - The following document specifies the query criteria where ``_id``
62
- is equal to ``1``:
86
+ - field ``name`` whose value is another *Document* that contains the
87
+ fields ``first`` and ``last``
88
+
89
+ - fields ``birth`` and ``death`` whose value is a *Date*
63
90
64
- .. code-block:: javascript
91
+ - field ``contribs`` whose value is an *array of Strings*
65
92
66
- { _id: 1 }
93
+ - field ``awards`` whose value is an *array of Documents*
67
94
68
- - The following document specifies the query criteria where ``_id``
69
- is greater than ``3``:
95
+ .. _documents-query-selectors:
70
96
71
- .. code-block:: javascript
97
+ Document as query selectors
98
+ ---------------------------
72
99
73
- { _id: { $gt: 3 } }
100
+ Query selector documents may contain any or all of the following:
74
101
75
- - The following document specifies the query criteria where ``_id``
76
- is equal to ``1`` **and** the ``name`` field equals the document ``{
77
- first: 'John', last: 'Backus' }``:
102
+ - Simple ``field:value`` pair(s) to specify the equality condition.
78
103
79
- .. code-block:: javascript
104
+ - :doc:`Query operator </reference/operators>` expressions to specify
105
+ other conditions.
80
106
81
- { _id: 1, name: { first: 'John', last: 'Backus' } }
107
+ Consider the following examples of query selector documents:
82
108
83
- When passed as an argument to methods such as the :method:`find()
84
- <db.collection.find()>` method or the :method:`update()
85
- <db.collection.update()>` method, the query document determines which
86
- records are returned or updated:
109
+ - The following document specifies the query criteria where ``_id`` is
110
+ equal to ``1``:
87
111
88
112
.. code-block:: javascript
89
113
90
- db.csbios.find( { _id: 1 } )
91
- db.csbios.find( { _id: { $gt: 3 } } )
92
- db.csbios.update( { _id: 1, name: { first: 'John', last: 'Backus' } },
93
- ... )
94
-
95
- - The following document specifies the update actions:
114
+ { _id: 1 }
115
+
116
+ - The following document specifies the query criteria where ``_id`` is
117
+ greater than ``3``:
96
118
97
119
.. code-block:: javascript
98
120
99
- { $set: { 'name.middle': 'Warner' },
100
- $push: { awards: { award: 'IBM Fellow',
101
- year: '1963',
102
- by: 'IBM' }
121
+ { _id: { $gt: 3 } }
103
122
104
- When passed as an argument to the :method:`update()
105
- <db.collection.update()>` method, the document specifies the update
106
- actions to perform on the document :
123
+ - The following document specifies the compound query criteria where
124
+ ``_id`` is equal to ``1`` **and** the ``name`` field equals the
125
+ document ``{ first: 'John', last: 'Backus' }`` :
107
126
108
127
.. code-block:: javascript
109
128
110
- db.csbios.update( { _id: 1 },
111
- { $set: { 'name.middle': 'Warner' },
112
- $push: { awards: { award: 'IBM Fellow',
113
- year: '1963',
114
- by: 'IBM' } } }
115
- )
129
+ { _id: 1, name: { first: 'John', last: 'Backus' } }
116
130
117
- - The following document specifies the sort order:
131
+ - The following document specifies the compound query criteria where
132
+ ``_id`` is equal to ``1`` **or** the ``name`` field equals the
133
+ document ``{ first: 'John', last: 'Backus' }``:
118
134
119
135
.. code-block:: javascript
120
136
121
- { 'name.last': 1, ' name. first':1 }
137
+ { $or: [ { _id: 1 }, { name: { first: 'John', last: 'Backus' } } ] }
122
138
123
- When passed as an argument to the :method:`sort() <cursor.sort()>`
124
- method, the document specifies the sort order of the results:
139
+ When passed as an argument to methods such as the :method:`find()
140
+ <db.collection.find()>` method, the :method:`remove()
141
+ <db.collection.remove()>` method, or the :method:`update()
142
+ <db.collection.update()>` method, the query selector document
143
+ determines which records are returned, removed, or updated:
125
144
126
- .. code-block:: javascript
145
+ .. code-block:: javascript
127
146
128
- db.csbios.find().sort( { 'name.last': 1, 'name.first': 1 } )
147
+ db.csbios.find( { _id: 1 } )
148
+ db.csbios.remove( { _id: { $gt: 3 } } )
149
+ db.csbios.update( { _id: 1, name: { first: 'John', last: 'Backus' } },
150
+ ... )
129
151
130
- - The following document specifies the multi-key index to create :
152
+ .. _documents-update-actions :
131
153
132
- .. code-block:: javascript
154
+ Document as update actions
155
+ --------------------------
133
156
134
- { _id:1, 'name.last': 1 }
157
+ The update actions documents contain :ref:`update operators
158
+ <update-operators>` expressions.
135
159
136
- When passed as an argument to the :method:`ensureIndex()
137
- <db.collection.ensureIndex()>` method, the document specifies the
138
- multi-key index to create on the fields ``_id`` and ``name.last``:
160
+ Consider the following example of an update actions document:
139
161
140
- .. code-block:: javascript
162
+ .. code-block:: javascript
163
+
164
+ { $set: { 'name.middle': 'Warner' },
165
+ $push: { awards: { award: 'IBM Fellow',
166
+ year: '1963',
167
+ by: 'IBM' }
168
+
169
+ When passed as an argument to the :method:`update()
170
+ <db.collection.update()>` method, the update actions document:
171
+
172
+ - Modifies the field ``name`` whose value is another document.
173
+ Specifically, the :operator:`$set` operator updates the ``middle``
174
+ field in the ``name`` subdocument.
175
+
176
+ - Adds an element to the field ``awards`` whose value is an array.
177
+ Specifically, the :operator:`$push` operator adds another document as
178
+ element to the field ``awards``.
179
+
180
+ .. code-block:: javascript
181
+
182
+ db.csbios.update( { _id: 1 },
183
+ { $set: { 'name.middle': 'Warner' },
184
+ $push: { awards: { award: 'IBM Fellow',
185
+ year: '1963',
186
+ by: 'IBM' } } }
187
+ )
188
+
189
+ .. _documents-index:
190
+
191
+ Document as index
192
+ -----------------
193
+
194
+ The index documents contain ``field:value`` pairs where:
195
+
196
+ - the ``field`` is the field to index on
197
+
198
+ - the ``value`` is either 1 for ascending or -1 for descending.
199
+
200
+ The following document specifies the multi-key index on the ``_id``
201
+ field and the ``last`` field contained in the subdocument ``name``
202
+ field:
203
+
204
+ .. code-block:: javascript
205
+
206
+ { _id:1, 'name.last': 1 }
207
+
208
+ When passed as an argument to the :method:`ensureIndex()
209
+ <db.collection.ensureIndex()>` method, the index documents specifies
210
+ the index to create:
211
+
212
+ .. code-block:: javascript
213
+
214
+ db.csbios.ensureIndex( { _id: 1, 'name.last': 1 } )
215
+
216
+ .. _documents-sort-order:
217
+
218
+ Document as sort order
219
+ ----------------------
220
+
221
+ The sort order documents contain ``field:value`` pairs where:
222
+
223
+ - the ``field`` is the field to sort
224
+
225
+ - the ``value`` is either 1 for ascending or -1 for descending.
226
+
227
+ The following document specifies the sort order using the fields from a
228
+ subdocument ``name``: first sort by the ``last`` field ascending, then
229
+ by the ``first`` field also ascending:
230
+
231
+ .. code-block:: javascript
232
+
233
+ { 'name.last': 1, 'name.first':1 }
234
+
235
+ When passed as an argument to the :method:`sort() <cursor.sort()>`
236
+ method, the sort order document sorts the results of the
237
+ :method:`find() <db.collection.find()>` method:
238
+
239
+ .. code-block:: javascript
240
+
241
+ db.csbios.find().sort( { 'name.last': 1, 'name.first': 1 } )
141
242
142
- db.csbios.ensureIndex( { _id: 1, 'name.last': 1 } )
0 commit comments