@@ -79,7 +79,7 @@ The document contains the following fields:
79
79
80
80
- ``_id`` that holds an *ObjectId*.
81
81
82
- - ``name`` that holds a *sub-document * that contains the fields
82
+ - ``name`` that holds a *subdocument * that contains the fields
83
83
``first`` and ``last``.
84
84
85
85
- ``birth`` and ``death``, which both have *Date* types.
@@ -246,6 +246,9 @@ Consider the following examples of query documents:
246
246
247
247
{ _id: 1, name: { first: 'John', last: 'Backus' } }
248
248
249
+ The ``name`` field must match the document exactly, including the
250
+ order of the fields.
251
+
249
252
- The following document specifies the compound query criteria where
250
253
``_id`` is equal to ``1`` **or** the ``name`` field equals the
251
254
document ``{ first: 'John', last: 'Backus' }``:
@@ -254,6 +257,79 @@ Consider the following examples of query documents:
254
257
255
258
{ $or: [ { _id: 1 }, { name: { first: 'John', last: 'Backus' } } ] }
256
259
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
+
257
333
When passed as an argument to methods such as the :method:`find()
258
334
<db.collection.find()>` method, the :method:`remove()
259
335
<db.collection.remove()>` method, or the :method:`update()
@@ -295,7 +371,8 @@ When passed as an argument to the :method:`update()
295
371
296
372
- Modifies the field ``name`` whose value is another document.
297
373
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.
299
376
300
377
- Adds an element to the field ``awards`` whose value is an array.
301
378
Specifically, the :operator:`$push` operator adds another document as
@@ -315,6 +392,10 @@ When passed as an argument to the :method:`update()
315
392
}
316
393
)
317
394
395
+ For additional examples of updates that involve array elements,
396
+ including where the elements are documents, see the :operator:`$`
397
+ positional operator.
398
+
318
399
.. _documents-index:
319
400
.. _document-index-specification:
320
401
@@ -335,9 +416,10 @@ Index documents contain field and value pairs, in the following form:
335
416
336
417
- ``value`` is either 1 for ascending or -1 for descending.
337
418
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:
341
423
342
424
.. code-block:: javascript
343
425
@@ -355,7 +437,6 @@ the index to create:
355
437
</core/read-operations>` and :doc:`write </core/write-operations>`
356
438
operations.
357
439
358
-
359
440
.. _documents-sort-order:
360
441
361
442
Sort Order Specification Documents
0 commit comments