44
55.. default-domain:: mongodb
66
7-
8-
97.. contents:: On this page
108 :local:
119 :backlinks: none
@@ -25,11 +23,8 @@ Definition
2523 :binary:`~bin.mongosh` provides the following helper methods:
2624
2725 - :method:`db.collection.updateOne()`
28-
2926 - :method:`db.collection.updateMany()`
3027
31- - :method:`db.collection.update()`
32-
3328Syntax
3429------
3530
@@ -74,65 +69,41 @@ The command takes the following fields:
7469 :widths: 20 20 80
7570
7671 * - Field
77-
7872 - Type
79-
8073 - Description
8174
8275 * - ``update``
83-
8476 - string
85-
8677 - The name of the target collection.
87-
88-
8978
9079 * - ``updates``
91-
9280 - array
93-
9481 - An array of one or more update statements to perform on the named
9582 collection. For details of the update statements, see :ref:`Update
9683 Statements <update-statement-documents>`.
97-
98-
9984
10085 * - ``ordered``
101-
10286 - boolean
103-
10487 - Optional. If ``true``, then when an update statement fails, return without
10588 performing the remaining update statements. If ``false``, then when
10689 an update fails, continue with the remaining update statements, if
10790 any. Defaults to ``true``.
108-
109-
11091
11192 * - ``writeConcern``
112-
11393 - document
114-
11594 - Optional. A document expressing the :doc:`write concern </reference/write-concern>`
11695 of the :dbcommand:`update` command. Omit to use the default write
11796 concern.
11897
11998 .. include:: /includes/extracts/transactions-operations-write-concern.rst
120-
121-
12299
123100 * - ``bypassDocumentValidation``
124-
125101 - boolean
126-
127102 - Optional. Enables :dbcommand:`update` to bypass document validation
128103 during the operation. This lets you update documents that do not
129104 meet the validation requirements.
130-
131- .. versionadded:: 3.2
132-
133-
134- * - ``comment``
135105
106+ * - ``comment``
136107 - any
137108
138109 - .. include:: /includes/extracts/comment-content.rst
@@ -141,13 +112,11 @@ The command takes the following fields:
141112
142113
143114 * - :ref:`let <update-let-syntax>`
144-
145115 - document
146-
147116 - .. _update-let-syntax:
148-
117+
149118 Optional.
150-
119+
151120 .. include:: /includes/let-variables-syntax.rst
152121
153122 For a complete MQL example, see :ref:`update-variables-example`.
@@ -167,50 +136,39 @@ Each document contains the following fields:
167136 :widths: 20 20 80
168137
169138 * - Field
170-
171139 - Type
172-
173140 - Description
174141
175142 * - :ref:`q <update-command-q>`
176-
177143 - document
178-
179144 - .. _update-command-q:
180145
181146 The query that matches documents to update. Use the same :ref:`query
182147 selectors <query-selectors>` as used in the :method:`find()
183148 <db.collection.find()>` method.
184149
185150 * - :ref:`u <update-command-u>`
186-
187151 - document or pipeline
188-
189152 - .. _update-command-u:
190153
191154 The modifications to apply.
192-
193155 The value can be either:
194156
195157 - A document that contains :ref:`update operator expressions
196158 <update-operators>`,
197-
198159 - A replacement document with only ``<field1>: <value1>`` pairs, or
199-
200160 - Starting in MongoDB 4.2, an aggregation pipeline.
201161
202162 .. include:: /includes/list-update-agg-stages.rst
203163
204164 For details, see :ref:`update-command-behaviors`.
205165
206166 * - :ref:`c <update-command-c>`
207-
208167 - document
209-
210168 - .. _update-command-c:
211169
212170 Optional.
213-
171+
214172 .. include:: /includes/let-variables-syntax.rst
215173
216174 .. include:: /includes/let-variables-syntax-note.rst
@@ -221,17 +179,13 @@ Each document contains the following fields:
221179 .. versionadded:: 5.0
222180
223181 * - :ref:`upsert <update-command-upsert>`
224-
225182 - boolean
226-
227183 - .. _update-command-upsert:
228184
229185 .. include:: /includes/extracts/update-upsert-behavior-command.rst
230186
231187 * - ``multi``
232-
233188 - boolean
234-
235189 - Optional. If ``true``, updates all documents that meet the query criteria. If
236190 ``false``, limit the update to one document that meet the query
237191 criteria. Defaults to ``false``.
@@ -776,11 +730,11 @@ values to calculate a separate field value.
776730
777731.. code-block:: javascript
778732
779- db.students.insert( [
733+ db.students.insertMany( [
780734 { "_id" : 1, "tests" : [ 95, 92, 90 ] },
781735 { "_id" : 2, "tests" : [ 94, 88, 90 ] },
782736 { "_id" : 3, "tests" : [ 70, 75, 82 ] }
783- ]);
737+ ] );
784738
785739Using an aggregation pipeline, you can update the documents with the
786740calculated grade average and letter grade.
@@ -924,24 +878,24 @@ documents:
924878
925879.. code-block:: javascript
926880
927- db.students.insert( [
881+ db.students.insertMany( [
928882 { "_id" : 1, "grades" : [ 95, 92, 90 ] },
929883 { "_id" : 2, "grades" : [ 98, 100, 102 ] },
930884 { "_id" : 3, "grades" : [ 95, 110, 100 ] }
931- ]);
885+ ] );
932886
933887To modify all elements that are greater than or equal to ``100`` in the
934888``grades`` array, use the filtered positional operator
935889:update:`$[\<identifier\>]` with the ``arrayFilters`` option:
936890
937891.. code-block:: javascript
938892
939- db.runCommand({
893+ db.runCommand( {
940894 update: "students",
941895 updates: [
942896 { q: { grades: { $gte: 100 } }, u: { $set: { "grades.$[element]" : 100 } }, arrayFilters: [ { "element": { $gte: 100 } } ], multi: true}
943897 ]
944- })
898+ } )
945899
946900After the operation, the collection contains the following documents:
947901
@@ -959,7 +913,7 @@ Create a collection ``students2`` with the following documents:
959913
960914.. code-block:: javascript
961915
962- db.students2.insert( [
916+ db.students2.insertMany( [
963917 {
964918 "_id" : 1,
965919 "grades" : [
@@ -976,7 +930,7 @@ Create a collection ``students2`` with the following documents:
976930 { "grade" : 85, "mean" : 85, "std" : 4 }
977931 ]
978932 }
979- ]);
933+ ] )
980934
981935To modify the value of the ``mean`` field for all elements in the
982936``grades`` array where the grade is greater than or equal to ``85``,
0 commit comments