Skip to content

Commit 1972467

Browse files
author
Sam Kleinman
committed
merge: DOCS-800
2 parents 7bbbd96 + cbd983a commit 1972467

File tree

6 files changed

+279
-97
lines changed

6 files changed

+279
-97
lines changed

source/applications/read.txt

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ Consider the following examples that illustrate the use of the
126126

127127
- The following operation returns all documents in the ``bios``
128128
collection where the array field ``contribs`` contains the element
129-
``UNIX``:
129+
``'UNIX'``:
130130

131131
.. code-block:: javascript
132132

@@ -135,11 +135,30 @@ Consider the following examples that illustrate the use of the
135135
contribs: 'UNIX'
136136
}
137137
)
138+
139+
- The following operation returns all documents in the ``bios``
140+
collection where ``awards`` array contains a subdocument element
141+
that contains the ``award`` field equal to ``'Turing Award'`` and the
142+
``year`` field greater than 1980:
143+
144+
.. code-block:: javascript
145+
146+
db.bios.find(
147+
{
148+
awards: {
149+
$elemMatch: {
150+
award: 'Turing Award',
151+
year: { $gt: 1980 }
152+
}
153+
}
154+
}
155+
)
138156

139157
- The following operation returns all documents in the ``bios``
140158
collection where the subdocument ``name`` contains a field
141-
``first`` with the value ``Yukihiro`` and a field ``last`` with the
142-
value ``Matsumoto``:
159+
``first`` with the value ``'Yukihiro'`` and a field ``last`` with
160+
the value ``'Matsumoto'``; the query uses :term:`dot notation` to
161+
access fields in a subdocument:
143162

144163
.. code-block:: javascript
145164

@@ -151,8 +170,8 @@ Consider the following examples that illustrate the use of the
151170
)
152171

153172
The query matches the document where the ``name`` field contains a
154-
subdocument with the field ``first`` with the value ``Yukihiro``
155-
and a field ``last`` with the value ``Matsumoto``. For instance,
173+
subdocument with the field ``first`` with the value ``'Yukihiro'``
174+
and a field ``last`` with the value ``'Matsumoto'``. For instance,
156175
the query would match documents with ``name`` fields that
157176
held either of the following values:
158177

@@ -230,23 +249,10 @@ Consider the following examples that illustrate the use of the
230249
}
231250
)
232251

233-
- The following operation returns all documents in the ``bios``
234-
collection where ``awards`` array contains a subdocument element
235-
that contains the ``award`` field equal to ``Turing Award`` and the
236-
``year`` field greater than 1980:
237-
238-
.. code-block:: javascript
239-
240-
db.bios.find(
241-
{
242-
awards: {
243-
$elemMatch: {
244-
award: 'Turing Award',
245-
year: { $gt: 1980 }
246-
}
247-
}
248-
}
249-
)
252+
In this query, the parameters (i.e. the selections of both fields)
253+
combine using an implicit logical AND for criteria on different
254+
fields ``contribs`` and ``name.first``. For multiple AND criteria
255+
on the same field, use the :operator:`$and` operator.
250256

251257
- If there is a ``<projection>`` argument, the :method:`find()
252258
<db.collection.find()>` method returns only those fields as specified
@@ -282,7 +288,7 @@ Consider the following examples that illustrate the use of the
282288

283289
- The following operation finds the documents in the ``bios``
284290
collection where the ``contribs`` field contains the element
285-
``OOP`` and returns all fields *except* the ``_id`` field, the
291+
``'OOP'`` and returns all fields *except* the ``_id`` field, the
286292
``first`` field in the ``name`` subdocument, and the ``birth``
287293
field from the matching documents:
288294

@@ -308,6 +314,16 @@ Consider the following examples that illustrate the use of the
308314
}
309315
)
310316

317+
.. seealso::
318+
319+
- :term:`dot notation` for information on "reaching into" embedded
320+
sub-documents.
321+
322+
- :ref:`read-operations-arrays` for more examples on accessing arrays
323+
324+
- :ref:`read-operations-subdocuments` for more examples on accessing
325+
subdocuments
326+
311327
Cursor
312328
~~~~~~
313329

source/applications/update.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ Consider the following examples that illustrate the use of the
7373
- If the ``<update>`` argument contains only :ref:`update operator
7474
<update-operators>` expressions such as the :operator:`$set` operator
7575
expression, the :method:`update() <db.collection.update()>` method
76-
updates the corresponding fields in the document.
76+
updates the corresponding fields in the document. To update fields in
77+
subdocuments, MongoDB uses :term:`dot notation`.
7778

7879
The following operation queries the ``bios`` collection for the first
7980
document that has an ``_id`` field equal to ``1`` and sets the field
@@ -161,8 +162,8 @@ Consider the following examples that illustrate the use of the
161162
field:
162163

163164
- The :method:`update() <db.collection.update()>` method can perform
164-
the update using the position of the element. Arrays in MongoDB are
165-
zero-based.
165+
the update using the position of the element and
166+
:term:`dot notation`. Arrays in MongoDB are zero-based.
166167

167168
The following operation queries the ``bios`` collection for
168169
the first document with ``_id`` field equal to ``1`` and updates the
@@ -196,8 +197,7 @@ Consider the following examples that illustrate the use of the
196197

197198
- The :method:`update() <db.collection.update()>` method can perform
198199
the update of an array that contains subdocuments by using the
199-
:operator:`$` positional operator and the :wiki:`dot notation
200-
<Dot+Notation+(Reaching+into+Objects)>`.
200+
positional operator (i.e. :operator:`$`) and the :term:`dot notation`.
201201

202202
The following operation queries the ``bios`` collection for the first
203203
document where the ``_id`` field equals ``6`` and the ``awards``

source/core/document.txt

Lines changed: 69 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ the following structure:
5656
fieldN: valueN
5757
}
5858

59-
Having support for the full range of :term:`BSON types`, MongoDB
60-
documents may contain field and value pairs where the value can be
61-
another document, an array, an array of documents as well as the basic
62-
types such as ``Double``, ``String``, and ``Date``. See also
59+
Having support for the full range of BSON types, MongoDB documents may
60+
contain field and value pairs where the value can be another document,
61+
an array, an array of documents as well as the basic types such as
62+
``Double``, ``String``, and ``Date``. See also
6363
:ref:`document-bson-type-considerations`.
6464

6565
Consider the following document that contains values of varying types:
@@ -79,7 +79,7 @@ The document contains the following fields:
7979

8080
- ``_id`` that holds an *ObjectId*.
8181

82-
- ``name`` that holds a *sub-document* that contains the fields
82+
- ``name`` that holds a *subdocument* that contains the fields
8383
``first`` and ``last``.
8484

8585
- ``birth`` and ``death``, which both have *Date* types.
@@ -88,6 +88,11 @@ The document contains the following fields:
8888

8989
- ``views`` that holds a value of *NumberLong* type.
9090

91+
.. _document-type-operators:
92+
93+
Type Operators
94+
~~~~~~~~~~~~~~
95+
9196
To determine the type of fields, the :program:`mongo` shell provides
9297
the following operators:
9398

@@ -119,6 +124,21 @@ the following operators:
119124
In this case ``typeof`` will return the more generic ``object``
120125
type rather than ``ObjectId`` type.
121126

127+
.. _document-dot-notation:
128+
129+
Dot Notation
130+
~~~~~~~~~~~~
131+
132+
.. include:: /includes/fact-dot-notation.rst
133+
134+
.. seealso::
135+
136+
- :ref:`read-operations-subdocuments` for dot notation examples
137+
with subdocuments.
138+
139+
- :ref:`read-operations-arrays` for dot notation examples with
140+
arrays.
141+
122142
Document Types in MongoDB
123143
-------------------------
124144

@@ -215,44 +235,10 @@ Query Specification Documents
215235
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
216236

217237
Query documents specify the conditions that determine which records to
218-
select for read, update, and delete operations. You can use field and
219-
value expressions to specify the equality condition and :doc:`query
220-
operator </reference/operators>` expressions to specify additional
221-
conditions. Refer to :doc:`read </applications/read>`, :doc:`update
222-
</applications/update>`, and :doc:`delete </applications/delete>` pages
223-
for more examples.
224-
225-
Consider the following examples of query documents:
226-
227-
- The following document specifies the query criteria where ``_id`` is
228-
equal to ``1``:
229-
230-
.. code-block:: javascript
231-
232-
{ _id: 1 }
233-
234-
- The following document specifies the query criteria where ``_id`` is
235-
greater than ``3``:
236-
237-
.. code-block:: javascript
238-
239-
{ _id: { $gt: 3 } }
240-
241-
- The following document specifies the compound query criteria where
242-
``_id`` is equal to ``1`` **and** the ``name`` field equals the
243-
document ``{ first: 'John', last: 'Backus' }``:
244-
245-
.. code-block:: javascript
246-
247-
{ _id: 1, name: { first: 'John', last: 'Backus' } }
248-
249-
- The following document specifies the compound query criteria where
250-
``_id`` is equal to ``1`` **or** the ``name`` field equals the
251-
document ``{ first: 'John', last: 'Backus' }``:
252-
253-
.. code-block:: javascript
254-
255-
{ $or: [ { _id: 1 }, { name: { first: 'John', last: 'Backus' } } ] }
238+
select for read, update, and delete operations. You can use
239+
``<field>:<value>`` expressions to specify the equality condition and
240+
:doc:`query operator </reference/operators>` expressions to specify
241+
additional conditions.
256242

257243
When passed as an argument to methods such as the :method:`find()
258244
<db.collection.find()>` method, the :method:`remove()
@@ -265,7 +251,20 @@ for MongoDB to return, remove, or update, as in the following:
265251
db.bios.find( { _id: 1 } )
266252
db.bios.remove( { _id: { $gt: 3 } } )
267253
db.bios.update( { _id: 1, name: { first: 'John', last: 'Backus' } },
268-
... )
254+
<update>,
255+
<options> )
256+
257+
.. seealso::
258+
259+
- :ref:`read-operations-query-argument` and
260+
:doc:`/applications/read` for more examples on selecting documents
261+
for reads.
262+
263+
- :doc:`/applications/update` for more examples on
264+
selecting documents for updates.
265+
266+
- :doc:`/applications/delete` for more examples on selecting
267+
documents for deletes.
269268

270269
.. _documents-update-actions:
271270

@@ -276,14 +275,14 @@ Update documents specify the data modifications to perform during
276275
an :method:`update() <db.collection.update()>` operation to modify
277276
existing records in a collection. You can use :ref:`update operators
278277
<update-operators>` to specify the exact actions to perform on the
279-
document fields. See the :ref:`update operators <update-operators>`
280-
page for the available update operators and syntax.
278+
document fields.
281279

282280
Consider the update document example:
283281

284282
.. code-block:: javascript
285283

286-
{ $set: { 'name.middle': 'Warner' },
284+
{
285+
$set: { 'name.middle': 'Warner' },
287286
$push: { awards: { award: 'IBM Fellow',
288287
year: '1963',
289288
by: 'IBM' }
@@ -295,7 +294,8 @@ When passed as an argument to the :method:`update()
295294

296295
- Modifies the field ``name`` whose value is another document.
297296
Specifically, the :operator:`$set` operator updates the ``middle``
298-
field in the ``name`` subdocument.
297+
field in the ``name`` subdocument. The document uses :ref:`dot
298+
notation <document-dot-notation>` to access a field in a subdocument.
299299

300300
- Adds an element to the field ``awards`` whose value is an array.
301301
Specifically, the :operator:`$push` operator adds another document as
@@ -305,7 +305,8 @@ When passed as an argument to the :method:`update()
305305

306306
db.bios.update(
307307
{ _id: 1 },
308-
{ $set: { 'name.middle': 'Warner' },
308+
{
309+
$set: { 'name.middle': 'Warner' },
309310
$push: { awards: {
310311
award: 'IBM Fellow',
311312
year: '1963',
@@ -315,6 +316,18 @@ When passed as an argument to the :method:`update()
315316
}
316317
)
317318

319+
.. seealso::
320+
321+
- :ref:`update operators <update-operators>` page for the available
322+
update operators and syntax.
323+
324+
- :doc:`update </applications/update>` for more examples on
325+
update documents.
326+
327+
For additional examples of updates that involve array elements,
328+
including where the elements are documents, see the :operator:`$`
329+
positional operator.
330+
318331
.. _documents-index:
319332
.. _document-index-specification:
320333

@@ -335,9 +348,11 @@ Index documents contain field and value pairs, in the following form:
335348

336349
- ``value`` is either 1 for ascending or -1 for descending.
337350

338-
The following document specifies the :ref:`multi-key index <index-type-multi-key>` on the ``_id``
339-
field and the ``last`` field contained in the subdocument ``name``
340-
field:
351+
The following document specifies the :ref:`multi-key index
352+
<index-type-multi-key>` on the ``_id`` field and the ``last`` field
353+
contained in the subdocument ``name`` field. The document uses
354+
:ref:`dot notation <document-dot-notation>` to access a field in a
355+
subdocument:
341356

342357
.. code-block:: javascript
343358

@@ -355,7 +370,6 @@ the index to create:
355370
</core/read-operations>` and :doc:`write </core/write-operations>`
356371
operations.
357372

358-
359373
.. _documents-sort-order:
360374

361375
Sort Order Specification Documents
@@ -397,8 +411,8 @@ method, the sort order document sorts the results of the
397411
.. _document-mongodb-type-considerations:
398412
.. _document-bson-type-considerations:
399413

400-
BSON Types
401-
----------
414+
BSON Type Considerations
415+
------------------------
402416

403417
The following BSON types require special consideration:
404418

0 commit comments

Comments
 (0)