Skip to content

Commit 54df7ab

Browse files
ianf-mongodbjeff-allen-mongo
authored andcommitted
DOCS-14158 Adding blurb to cover expression operator and upsert flag.
1 parent 8454aef commit 54df7ab

File tree

1 file changed

+10
-93
lines changed

1 file changed

+10
-93
lines changed

source/reference/method/db.collection.update.txt

Lines changed: 10 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@ privileges.
252252
Behavior
253253
--------
254254

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+
255261
.. _update-sharded-collection:
256262

257263
Sharded Collections
@@ -412,6 +418,10 @@ Examples
412418
The following tabs showcase a variety of common
413419
:method:`~db.collection.update()` operations.
414420

421+
.. include:: /includes/fact-update-example-docs-intro.rst
422+
423+
.. include:: /includes/fact-update-example-docs.rst
424+
415425
.. tabs::
416426

417427
.. tab:: Set
@@ -420,10 +430,6 @@ The following tabs showcase a variety of common
420430
Use Update Operator Expressions (``$inc`` and ``$set``)
421431
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
422432

423-
.. include:: /includes/fact-update-example-docs-intro.rst
424-
425-
.. include:: /includes/fact-update-example-docs.rst
426-
427433
If the ``<update>`` document contains :ref:`update operator
428434
<update-operators>` modifiers, such as those using the
429435
:update:`$set` modifier, then:
@@ -524,10 +530,6 @@ The following tabs showcase a variety of common
524530
Push Elements to Existing Array (``$push``)
525531
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
526532

527-
.. include:: /includes/fact-update-example-docs-intro.rst
528-
529-
.. include:: /includes/fact-update-example-docs.rst
530-
531533
The following operation uses the :update:`$push` update
532534
operator to append a new object to the ``ratings`` array.
533535

@@ -576,10 +578,6 @@ The following tabs showcase a variety of common
576578
Remove Fields (``$unset``)
577579
~~~~~~~~~~~~~~~~~~~~~~~~~~
578580

579-
.. include:: /includes/fact-update-example-docs-intro.rst
580-
581-
.. include:: /includes/fact-update-example-docs.rst
582-
583581
The following operation uses the :update:`$unset` operator to remove
584582
the ``tags`` field from the document with ``{ _id: 1 }``.
585583

@@ -622,93 +620,12 @@ The following tabs showcase a variety of common
622620

623621
:update:`$unset`, :update:`$rename`, :doc:`/reference/operator/update`
624622

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-
702623
.. tab:: Multiple
703624
:tabid: update-multiple
704625

705626
Update Multiple Documents (``$update`` With ``multi``)
706627
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
707628

708-
.. include:: /includes/fact-update-example-docs-intro.rst
709-
710-
.. include:: /includes/fact-update-example-docs.rst
711-
712629
If ``multi`` is set to ``true``, the
713630
:method:`db.collection.update()` method updates *all* documents
714631
that meet the ``<query>`` criteria. The ``multi`` update

0 commit comments

Comments
 (0)