@@ -252,6 +252,12 @@ privileges.
252
252
Behavior
253
253
--------
254
254
255
+ Using ``$expr`` in an Update with ``Upsert``
256
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
257
+
258
+ Attempting to use the :manual:`$expr </reference/operator/query/expr/>`
259
+ operator with the upsert flag set to ``true`` will generate an error.
260
+
255
261
.. _update-sharded-collection:
256
262
257
263
Sharded Collections
@@ -412,6 +418,10 @@ Examples
412
418
The following tabs showcase a variety of common
413
419
:method:`~db.collection.update()` operations.
414
420
421
+ .. include:: /includes/fact-update-example-docs-intro.rst
422
+
423
+ .. include:: /includes/fact-update-example-docs.rst
424
+
415
425
.. tabs::
416
426
417
427
.. tab:: Set
@@ -420,10 +430,6 @@ The following tabs showcase a variety of common
420
430
Use Update Operator Expressions (``$inc`` and ``$set``)
421
431
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
422
432
423
- .. include:: /includes/fact-update-example-docs-intro.rst
424
-
425
- .. include:: /includes/fact-update-example-docs.rst
426
-
427
433
If the ``<update>`` document contains :ref:`update operator
428
434
<update-operators>` modifiers, such as those using the
429
435
:update:`$set` modifier, then:
@@ -524,10 +530,6 @@ The following tabs showcase a variety of common
524
530
Push Elements to Existing Array (``$push``)
525
531
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
526
532
527
- .. include:: /includes/fact-update-example-docs-intro.rst
528
-
529
- .. include:: /includes/fact-update-example-docs.rst
530
-
531
533
The following operation uses the :update:`$push` update
532
534
operator to append a new object to the ``ratings`` array.
533
535
@@ -576,10 +578,6 @@ The following tabs showcase a variety of common
576
578
Remove Fields (``$unset``)
577
579
~~~~~~~~~~~~~~~~~~~~~~~~~~
578
580
579
- .. include:: /includes/fact-update-example-docs-intro.rst
580
-
581
- .. include:: /includes/fact-update-example-docs.rst
582
-
583
581
The following operation uses the :update:`$unset` operator to remove
584
582
the ``tags`` field from the document with ``{ _id: 1 }``.
585
583
@@ -622,93 +620,12 @@ The following tabs showcase a variety of common
622
620
623
621
:update:`$unset`, :update:`$rename`, :doc:`/reference/operator/update`
624
622
625
- .. tab:: Replace
626
- :tabid: replace-doc
627
-
628
- Replace Entire Document (``$update``)
629
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
630
-
631
- .. include:: /includes/fact-update-example-docs-intro.rst
632
-
633
- .. include:: /includes/fact-update-example-docs.rst
634
-
635
- If the ``<update>`` document contains *only* ``field:value``
636
- expressions, then:
637
-
638
- - The :method:`db.collection.update()` method *replaces* the matching
639
- document with the ``<update>`` document. The
640
- :method:`db.collection.update()` method *does not* replace the
641
- ``_id`` value.
642
-
643
- - :method:`db.collection.update()` *cannot* update multiple
644
- documents.
645
-
646
- The following operation passes an ``<update>`` document that contains
647
- only field and value pairs. The ``<update>`` document completely
648
- replaces the original document except for the ``_id`` field.
649
-
650
- You can use the web shell below to insert the sample
651
- documents and execute the example update operation:
652
-
653
- .. include:: /includes/fact-mws.rst
654
-
655
- .. code-block:: javascript
656
-
657
- db.books.update(
658
- { _id: 2 },
659
- {
660
- item: "XYZ123",
661
- stock: 10,
662
- info: { publisher: "2255", pages: 150 },
663
- tags: [ "baking", "cooking" ]
664
- }
665
- )
666
-
667
- The updated document contains *only* the fields from the
668
- replacement document and the ``_id`` field. As such, the fields
669
- ``ratings`` and ``reorder`` no longer exist in the updated
670
- document since the fields were not in the replacement document.
671
-
672
- .. code-block:: javascript
673
-
674
- {
675
- "_id" : 2,
676
- "item" : "XYZ123",
677
- "stock" : 10,
678
- "info" : { "publisher" : "2255", "pages" : 150 },
679
- "tags" : [ "baking", "cooking" ]
680
- }
681
-
682
- This operation corresponds to the following SQL statements:
683
-
684
- .. code-block:: sql
685
-
686
- DELETE from books WHERE _id = 2
687
-
688
- INSERT INTO books
689
- (_id,
690
- item,
691
- stock,
692
- publisher,
693
- pages,
694
- tags)
695
- VALUES (2,
696
- "xyz123",
697
- 10,
698
- "2255",
699
- 150,
700
- "baking,cooking")
701
-
702
623
.. tab:: Multiple
703
624
:tabid: update-multiple
704
625
705
626
Update Multiple Documents (``$update`` With ``multi``)
706
627
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
707
628
708
- .. include:: /includes/fact-update-example-docs-intro.rst
709
-
710
- .. include:: /includes/fact-update-example-docs.rst
711
-
712
629
If ``multi`` is set to ``true``, the
713
630
:method:`db.collection.update()` method updates *all* documents
714
631
that meet the ``<query>`` criteria. The ``multi`` update
0 commit comments