Skip to content

Commit db04398

Browse files
author
Dave
authored
DOCSP-20008 updates for update pt4 v5.1 (#129)
* DOCSP-20008 updates for update pt4 v5.2 (#119) * DOCSP-20008 updates for update pt4 * Add files * Fix multi * Add pull files * Staging fixes * Review updates * Fix bullets
1 parent 50210db commit db04398

File tree

9 files changed

+439
-390
lines changed

9 files changed

+439
-390
lines changed

source/reference/operator/update/currentDate.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ After the operation, you can query the collection to verify the update:
8686

8787
.. code-block:: javascript
8888

89-
db.customers.find().pretty()
89+
db.customers.find()
9090

9191
The updated document would resemble:
9292

@@ -160,6 +160,5 @@ The query should return the following document:
160160

161161
- :method:`db.collection.updateOne()`
162162
- :method:`db.collection.updateMany()`
163-
- :method:`db.collection.update()`
164163
- :method:`db.collection.findAndModify()`
165164
- :method:`db.collection.findOneAndUpdate()`

source/reference/operator/update/min.txt

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ Examples
4747
Use ``$min`` to Compare Numbers
4848
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4949

50-
Consider the following document in the collection ``scores``:
50+
Create the ``scores`` collection:
5151

5252
.. code-block:: javascript
5353

54-
{ _id: 1, highScore: 800, lowScore: 200 }
54+
db.scores.insertOne( { _id: 1, highScore: 800, lowScore: 200 } )
5555

5656
The ``lowScore`` for the document currently has the value
5757
``200``. The following operation uses :update:`$min` to compare
@@ -60,7 +60,7 @@ The ``lowScore`` for the document currently has the value
6060

6161
.. code-block:: javascript
6262

63-
db.scores.update( { _id: 1 }, { $min: { lowScore: 150 } } )
63+
db.scores.updateOne( { _id: 1 }, { $min: { lowScore: 150 } } )
6464

6565
The ``scores`` collection now contains the following modified document:
6666

@@ -73,7 +73,7 @@ field ``lowScore``, i.e ``150``, is less than ``250``:
7373

7474
.. code-block:: javascript
7575

76-
db.scores.update( { _id: 1 }, { $min: { lowScore: 250 } } )
76+
db.scores.updateOne( { _id: 1 }, { $min: { lowScore: 250 } } )
7777

7878
The document remains unchanged in the ``scores`` collection:
7979

@@ -84,16 +84,18 @@ The document remains unchanged in the ``scores`` collection:
8484
Use ``$min`` to Compare Dates
8585
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8686

87-
Consider the following document in the collection ``tags``:
87+
Create the ``tags`` collection:
8888

8989
.. code-block:: javascript
9090

91-
{
92-
_id: 1,
93-
desc: "crafts",
94-
dateEntered: ISODate("2013-10-01T05:00:00Z"),
95-
dateExpired: ISODate("2013-10-01T16:38:16Z")
96-
}
91+
db.tags.insertOne(
92+
{
93+
_id: 1,
94+
desc: "crafts",
95+
dateEntered: ISODate("2013-10-01T05:00:00Z"),
96+
dateExpired: ISODate("2013-10-01T16:38:16Z")
97+
}
98+
)
9799

98100
The following operation compares the current value of the
99101
``dateEntered`` field, i.e. ``ISODate("2013-10-01T05:00:00Z")``,
@@ -102,7 +104,7 @@ whether to update the field:
102104

103105
.. code-block:: javascript
104106

105-
db.tags.update(
107+
db.tags.updateOne(
106108
{ _id: 1 },
107109
{ $min: { dateEntered: new Date("2013-09-25") } }
108110
)
@@ -121,5 +123,6 @@ The operation updates the ``dateEntered`` field:
121123

122124
.. seealso::
123125

124-
- :method:`db.collection.update()`
126+
- :method:`db.collection.updateMany()`
125127
- :method:`db.collection.findAndModify()`
128+

source/reference/operator/update/position.txt

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,18 @@ Examples
6969
Add Elements at the Start of the Array
7070
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7171

72-
Consider a collection ``students`` that contains the following document:
72+
Create the ``students`` collection:
7373

7474
.. code-block:: javascript
7575

76-
{ "_id" : 1, "scores" : [ 100 ] }
76+
db.students.insertOne( { "_id" : 1, "scores" : [ 100 ] } )
7777

7878
The following operation updates the ``scores`` field to add the
7979
elements ``50``, ``60`` and ``70`` to the beginning of the array:
8080

8181
.. code-block:: javascript
8282

83-
db.students.update(
83+
db.students.updateOne(
8484
{ _id: 1 },
8585
{
8686
$push: {
@@ -101,19 +101,19 @@ The operation results in the following updated document:
101101
Add Elements to the Middle of the Array
102102
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
103103

104-
Consider a collection ``students`` that contains the following document:
104+
Add a document to the ``students`` collection:
105105

106106
.. code-block:: javascript
107107

108-
{ "_id" : 1, "scores" : [ 50, 60, 70, 100 ] }
108+
db.students.insertOne( { "_id" : 2, "scores" : [ 50, 60, 70, 100 ] } )
109109

110110
The following operation updates the ``scores`` field to add the
111111
elements ``20`` and ``30`` at the array index of ``2``:
112112

113113
.. code-block:: javascript
114114

115-
db.students.update(
116-
{ _id: 1 },
115+
db.students.updateOne(
116+
{ _id: 2 },
117117
{
118118
$push: {
119119
scores: {
@@ -128,24 +128,23 @@ The operation results in the following updated document:
128128

129129
.. code-block:: javascript
130130

131-
{ "_id" : 1, "scores" : [ 50, 60, 20, 30, 70, 100 ] }
131+
{ "_id" : 2, "scores" : [ 50, 60, 20, 30, 70, 100 ] }
132132

133133
Use a Negative Index to Add Elements to the Array
134134
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
135135

136-
.. versionchanged:: 3.6
136+
:update:`$position` can accept a negative array index value to indicate
137+
the position starting from the end, counting from (but not including)
138+
the last element of the array. For example, ``-1`` indicates the
139+
position just before the last element in the array.
137140

138-
:update:`$position` can accept a negative array index value to
139-
indicate the position starting from the end, counting from (but not
140-
including) the last element of the array. For example, ``-1``
141-
indicates the position just before the last element in the array.
142-
143-
Consider a collection ``students`` that contains the following document:
141+
Add the following document to the ``students`` collection:
144142

145143
.. code-block:: javascript
146144

147-
{ "_id" : 1, "scores" : [ 50, 60, 20, 30, 70, 100 ] }
148-
145+
db.students.insertOne(
146+
{ "_id" : 3, "scores" : [ 50, 60, 20, 30, 70, 100 ] }
147+
)
149148

150149
The following operation specifies ``-2`` for the :update:`$position` to
151150
add ``90`` at the position two places before the last element, and then
@@ -159,8 +158,8 @@ add ``90`` at the position two places before the last element, and then
159158

160159
.. code-block:: javascript
161160

162-
db.students.update(
163-
{ _id: 1 },
161+
db.students.updateOne(
162+
{ _id: 3 },
164163
{
165164
$push: {
166165
scores: {
@@ -175,4 +174,5 @@ The operation results in the following updated document:
175174

176175
.. code-block:: javascript
177176

178-
{ "_id" : 1, "scores" : [ 50, 60, 20, 30, 90, 80, 70, 100 ] }
177+
{ "_id" : 3, "scores" : [ 50, 60, 20, 30, 90, 80, 70, 100 ] }
178+

source/reference/operator/update/positional-all.txt

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ Definition
2424

2525
{ <update operator>: { "<array>.$[]" : value } }
2626

27-
Use in update operations, e.g. :method:`db.collection.update()` and
28-
:method:`db.collection.findAndModify()`, to modify all array
27+
Use in update operations, e.g. :method:`db.collection.updateOne()`
28+
and :method:`db.collection.findAndModify()`, to modify all array
2929
elements for the document or documents that match the query
3030
condition. For example:
3131

3232
.. code-block:: javascript
3333

34-
db.collection.updateMany(
34+
db.collection.updateOne(
3535
{ <query conditions> },
3636
{ <update operator>: { "<array>.$[]" : value } }
3737
)
@@ -57,7 +57,7 @@ array field:
5757

5858
.. code-block:: javascript
5959

60-
db.collection.update(
60+
db.collection.updateOne(
6161
{ myArray: [ 5, 8 ] },
6262
{ $set: { "myArray.$[]": 10 } },
6363
{ upsert: true }
@@ -79,23 +79,23 @@ documents were found to update:
7979

8080
.. code-block:: javascript
8181

82-
db.collection.update(
83-
{ myArray: 5 },
82+
db.emptyCollection.updateOne(
83+
{ },
8484
{ $set: { "myArray.$[]": 10 } },
8585
{ upsert: true }
8686
)
87-
88-
db.collection.update(
89-
{ },
87+
88+
db.emptyCollection.updateOne(
89+
{ myArray: 5 },
9090
{ $set: { "myArray.$[]": 10 } },
9191
{ upsert: true }
9292
)
9393

9494
Nested Arrays
9595
~~~~~~~~~~~~~
9696

97-
The ``$[]`` operator can be used for queries which
98-
traverse more than one array and nested arrays.
97+
The ``$[]`` operator can be used for queries that traverse more than
98+
one array and nested arrays.
9999

100100
For an example, see :ref:`position-nested-arrays`.
101101

@@ -107,28 +107,29 @@ Examples
107107
Update All Elements in an Array
108108
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
109109

110-
Consider a collection ``students`` with the following documents:
110+
Create the ``students`` collection:
111111

112112
.. code-block:: javascript
113113

114-
{ "_id" : 1, "grades" : [ 85, 82, 80 ] }
115-
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }
116-
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
114+
db.students.insertMany( [
115+
{ "_id" : 1, "grades" : [ 85, 82, 80 ] },
116+
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
117+
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
118+
] )
117119

118120
To increment all elements in the ``grades`` array by ``10`` for all
119121
documents in the collection, use the all positional :update:`$[]`
120122
operator:
121123

122124
.. code-block:: javascript
123125

124-
db.students.update(
126+
db.students.updateMany(
125127
{ },
126128
{ $inc: { "grades.$[]": 10 } },
127-
{ multi: true }
128129
)
129130

130-
The all positional :update:`$[]` operator acts as a
131-
placeholder for all elements in the array field.
131+
The all positional :update:`$[]` operator acts as a placeholder for all
132+
elements in the array field.
132133

133134
After the operation, the ``students`` collection contains the following
134135
documents:
@@ -143,48 +144,49 @@ Update All Documents in an Array
143144
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
144145

145146
The :update:`$[]` positional operator facilitates updates to arrays
146-
that contain embedded documents. To access the fields
147-
in the embedded documents, use the :ref:`dot notation
148-
<document-dot-notation>` on the :update:`$[]` operator.
147+
that contain embedded documents. To access the fields in the embedded
148+
documents, use the :ref:`dot notation <document-dot-notation>` with the
149+
:update:`$[]` operator.
149150

150151
.. code-block:: javascript
151152

152-
db.collection.update(
153+
db.collection.updateOne(
153154
{ <query selector> },
154155
{ <update operator>: { "array.$[].field" : value } }
155156
)
156157

157158

158-
Consider a collection ``students2`` with the following documents:
159+
Create the ``students2`` collection:
159160

160161
.. code-block:: javascript
161162

162-
{
163-
"_id" : 1,
164-
"grades" : [
165-
{ "grade" : 80, "mean" : 75, "std" : 8 },
166-
{ "grade" : 85, "mean" : 90, "std" : 6 },
167-
{ "grade" : 85, "mean" : 85, "std" : 8 }
168-
]
169-
}
170-
{
171-
"_id" : 2,
172-
"grades" : [
173-
{ "grade" : 90, "mean" : 75, "std" : 8 },
174-
{ "grade" : 87, "mean" : 90, "std" : 5 },
175-
{ "grade" : 85, "mean" : 85, "std" : 6 }
176-
]
177-
}
163+
db.students2.insertMany( [
164+
{
165+
"_id" : 1,
166+
"grades" : [
167+
{ "grade" : 80, "mean" : 75, "std" : 8 },
168+
{ "grade" : 85, "mean" : 90, "std" : 6 },
169+
{ "grade" : 85, "mean" : 85, "std" : 8 }
170+
]
171+
},
172+
{
173+
"_id" : 2,
174+
"grades" : [
175+
{ "grade" : 90, "mean" : 75, "std" : 8 },
176+
{ "grade" : 87, "mean" : 90, "std" : 5 },
177+
{ "grade" : 85, "mean" : 85, "std" : 6 }
178+
]
179+
}
180+
] )
178181

179182
To modify the value of the ``std`` field for all elements in the
180183
``grades`` array, use the positional :update:`$[]` operator:
181184

182185
.. code-block:: javascript
183186

184-
db.students2.update(
187+
db.students2.updateMany(
185188
{ },
186189
{ $inc: { "grades.$[].std" : -2 } },
187-
{ multi: true }
188190
)
189191

190192
After the operation, the collection has the following documents:
@@ -211,24 +213,25 @@ After the operation, the collection has the following documents:
211213
Update Arrays Specified Using a Negation Query Operator
212214
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
213215

214-
Consider a collection ``results`` with the following documents:
216+
Create the ``results`` collection:
215217

216218
.. code-block:: javascript
217219

218-
{ "_id" : 1, "grades" : [ 85, 82, 80 ] }
219-
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }
220-
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
220+
db.results.insertMany( [
221+
{ "_id" : 1, "grades" : [ 85, 82, 80 ] },
222+
{ "_id" : 2, "grades" : [ 88, 90, 92 ] },
223+
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
224+
] )
221225

222226
To increment all elements in the ``grades`` array by ``10`` for all
223227
documents **except** those with the value ``100`` in the ``grades``
224228
array, use the all positional :update:`$[]` operator:
225229

226230
.. code-block:: javascript
227231

228-
db.results.update(
232+
db.results.updateMany(
229233
{ "grades" : { $ne: 100 } },
230234
{ $inc: { "grades.$[]": 10 } },
231-
{ multi: true }
232235
)
233236

234237
The all positional :update:`$[]` operator acts as a
@@ -275,7 +278,7 @@ nested ``grades.questions`` array, regardless of ``type``:
275278
db.students3.updateMany(
276279
{},
277280
{ $inc: { "grades.$[].questions.$[score]": 2 } },
278-
{ arrayFilters: [ { "score": { $gte: 8 } } ], multi: true}
281+
{ arrayFilters: [ { "score": { $gte: 8 } } ] }
279282
)
280283

281284
The updated documents look like this:

0 commit comments

Comments
 (0)