Skip to content

Commit ab47d24

Browse files
committed
DOCS-800 added dot notation documentation
1 parent b3b83c4 commit ab47d24

File tree

5 files changed

+293
-45
lines changed

5 files changed

+293
-45
lines changed

source/applications/read.txt

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,11 @@ Consider the following examples that illustrate the use of the
124124
}
125125
)
126126

127+
*Arrays*
128+
127129
- The following operation returns all documents in the ``bios``
128130
collection where the array field ``contribs`` contains the element
129-
``UNIX``:
131+
``'UNIX'``:
130132

131133
.. code-block:: javascript
132134

@@ -135,11 +137,36 @@ Consider the following examples that illustrate the use of the
135137
contribs: 'UNIX'
136138
}
137139
)
140+
141+
- The following operation returns all documents in the ``bios``
142+
collection where ``awards`` array contains a subdocument element
143+
that contains the ``award`` field equal to ``'Turing Award'`` and the
144+
``year`` field greater than 1980:
145+
146+
.. code-block:: javascript
147+
148+
db.bios.find(
149+
{
150+
awards: {
151+
$elemMatch: {
152+
award: 'Turing Award',
153+
year: { $gt: 1980 }
154+
}
155+
}
156+
}
157+
)
158+
159+
For more examples on accessing arrays, see :ref:`query documents
160+
for arrays <document-query-arrays>` and :ref:`read operations
161+
<read-operations-dot-notation-arrays>`.
162+
163+
*Subdocuments*
138164

139165
- The following operation returns all documents in the ``bios``
140166
collection where the subdocument ``name`` contains a field
141-
``first`` with the value ``Yukihiro`` and a field ``last`` with the
142-
value ``Matsumoto``:
167+
``first`` with the value ``'Yukihiro'`` and a field ``last`` with
168+
the value ``'Matsumoto'``; the query uses :term:`dot-notation` to
169+
access fields in a subdocument:
143170

144171
.. code-block:: javascript
145172

@@ -151,8 +178,8 @@ Consider the following examples that illustrate the use of the
151178
)
152179

153180
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,
181+
subdocument with the field ``first`` with the value ``'Yukihiro'``
182+
and a field ``last`` with the value ``'Matsumoto'``. For instance,
156183
the query would match documents with ``name`` fields that
157184
held either of the following values:
158185

@@ -201,6 +228,12 @@ Consider the following examples that illustrate the use of the
201228
first: 'Yukihiro'
202229
}
203230

231+
For more examples on accessing subdocuments, see :ref:`query
232+
documents for subdocuments <document-query-subdocuments>` and
233+
:ref:`read operations <read-operations-dot-notation-subdocuments>`.
234+
235+
*Compound Query Criteria*
236+
204237
- The following operation returns all documents in the ``bios``
205238
collection where either the field ``first`` in the sub-document
206239
``name`` starts with the letter ``G`` **or** where the field
@@ -230,24 +263,6 @@ Consider the following examples that illustrate the use of the
230263
}
231264
)
232265

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-
)
250-
251266
- If there is a ``<projection>`` argument, the :method:`find()
252267
<db.collection.find()>` method returns only those fields as specified
253268
in the ``<projection>`` argument to include or exclude:
@@ -282,7 +297,7 @@ Consider the following examples that illustrate the use of the
282297

283298
- The following operation finds the documents in the ``bios``
284299
collection where the ``contribs`` field contains the element
285-
``OOP`` and returns all fields *except* the ``_id`` field, the
300+
``'OOP'`` and returns all fields *except* the ``_id`` field, the
286301
``first`` field in the ``name`` subdocument, and the ``birth``
287302
field from the matching documents:
288303

source/applications/update.txt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ Consider the following examples that illustrate the use of the
161161
field:
162162

163163
- 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.
164+
the update using the position of the element and
165+
:term:`dot-notation`. Arrays in MongoDB are zero-based.
166166

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

197197
- The :method:`update() <db.collection.update()>` method can perform
198198
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)>`.
199+
:operator:`$` positional operator and the :term:`dot-notation`.
201200

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

source/core/document.txt

Lines changed: 87 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
@@ -246,6 +246,9 @@ Consider the following examples of query documents:
246246

247247
{ _id: 1, name: { first: 'John', last: 'Backus' } }
248248

249+
The ``name`` field must match the document exactly, including the
250+
order of the fields.
251+
249252
- The following document specifies the compound query criteria where
250253
``_id`` is equal to ``1`` **or** the ``name`` field equals the
251254
document ``{ first: 'John', last: 'Backus' }``:
@@ -254,6 +257,79 @@ Consider the following examples of query documents:
254257

255258
{ $or: [ { _id: 1 }, { name: { first: 'John', last: 'Backus' } } ] }
256259

260+
The ``name`` field must match the document exactly, including the
261+
order of the fields.
262+
263+
.. _document-query-arrays:
264+
265+
*Query Documents for Arrays*
266+
267+
You can specify query criteria for arrays at the array level and, with
268+
:term:`dot-notation`, at the element level:
269+
270+
- The following document specifies the query criteria where
271+
``contribs`` matches exactly the array ``[ 'Fortran', 'ALGOL',
272+
'Backus-Naur Form', 'FP' ]``, including the order of the elements:
273+
274+
.. code-block:: javascript
275+
276+
{ contribs: [ 'Fortran', 'ALGOL', 'Backus-Naur Form', 'FP' ] }
277+
278+
- The following document specifies the query criteria where the value
279+
of ``contribs`` is an array that contains as one of its element
280+
``'ALGOL'``:
281+
282+
.. code-block:: javascript
283+
284+
{ contribs: 'ALGOL' }
285+
286+
- The following document specifies the query criteria where
287+
``contribs`` contains an element ``'ALGOL'`` in the second position.
288+
Array indexes are zero-based:
289+
290+
.. code-block:: javascript
291+
292+
{ 'contribs.1': 'ALGOL' }
293+
294+
See :ref:`read operations <read-operations-dot-notation-arrays>` for more
295+
examples with arrays.
296+
297+
.. _document-query-subdocuments:
298+
299+
*Query Documents for Subdocuments*
300+
301+
You can specify query criteria for subdocuments at the subdocument
302+
level and, with :term:`dot-notation`, at the field level:
303+
304+
- The following document specifies the query criteria where the value
305+
of the field ``name`` is a subdocument that contains *only* the field
306+
``first`` equal to ``'John'`` and the field ``last`` equal to
307+
``'Backus'``, in that order.
308+
309+
.. code-block:: javascript
310+
311+
{ name: { first: 'John', last: 'Backus' } }
312+
313+
- The following document specifies the query criteria where the value
314+
of the field ``name`` is a subdocument that contains the field
315+
``first`` equal to ``'John'`` and may contain other fields:
316+
317+
.. code-block:: javascript
318+
319+
{ 'name.first': 'John' }
320+
321+
- The following document specifies the query criteria where the value
322+
of the field ``awards`` is an array that contains, as one of its
323+
elements, a subdocument that contains the field ``award`` equal to
324+
``'National Medal of Science'`` and may contain other fields:
325+
326+
.. code-block:: javascript
327+
328+
{ 'awards.award': 'National Medal of Science' }
329+
330+
See :ref:`read operations <read-operations-dot-notation-subdocuments>` for
331+
more examples with subdocuments.
332+
257333
When passed as an argument to methods such as the :method:`find()
258334
<db.collection.find()>` method, the :method:`remove()
259335
<db.collection.remove()>` method, or the :method:`update()
@@ -295,7 +371,8 @@ When passed as an argument to the :method:`update()
295371

296372
- Modifies the field ``name`` whose value is another document.
297373
Specifically, the :operator:`$set` operator updates the ``middle``
298-
field in the ``name`` subdocument.
374+
field in the ``name`` subdocument. The document uses
375+
:term:`dot-notation` to access a field in a subdocument.
299376

300377
- Adds an element to the field ``awards`` whose value is an array.
301378
Specifically, the :operator:`$push` operator adds another document as
@@ -315,6 +392,10 @@ When passed as an argument to the :method:`update()
315392
}
316393
)
317394

395+
For additional examples of updates that involve array elements,
396+
including where the elements are documents, see the :operator:`$`
397+
positional operator.
398+
318399
.. _documents-index:
319400
.. _document-index-specification:
320401

@@ -335,9 +416,10 @@ Index documents contain field and value pairs, in the following form:
335416

336417
- ``value`` is either 1 for ascending or -1 for descending.
337418

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:
419+
The following document specifies the :ref:`multi-key index
420+
<index-type-multi-key>` on the ``_id`` field and the ``last`` field
421+
contained in the subdocument ``name`` field; the document uses
422+
:term:`dot-notation` to access a field in a subdocument:
341423

342424
.. code-block:: javascript
343425

@@ -355,7 +437,6 @@ the index to create:
355437
</core/read-operations>` and :doc:`write </core/write-operations>`
356438
operations.
357439

358-
359440
.. _documents-sort-order:
360441

361442
Sort Order Specification Documents

0 commit comments

Comments
 (0)