Skip to content

Commit 708020b

Browse files
author
Dave
authored
DOCSP-20026 updates for update pt6 v5.2 (#136) (#139)
* DOCSP-20026 updates for update pt6 * Add mul * Still in mul * Add files * Add files * Add files * Staging updates * Review updates * Staging tweaks
1 parent 41645b6 commit 708020b

File tree

11 files changed

+241
-151
lines changed

11 files changed

+241
-151
lines changed

source/includes/example-addToSet-each.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ with the :update:`$each` modifier to add multiple elements to the
1515
{ $addToSet: { tags: { $each: [ "camera", "electronics", "accessories" ] } } }
1616
)
1717
18-
The operation adds only ``"camera"`` and ``"accessories"`` to the
19-
``tags`` array since ``"electronics"`` already exists in the array:
18+
The operation only adds ``"camera"`` and ``"accessories"`` to the
19+
``tags`` array. ``"electronics"`` was already in the array:
2020

2121
.. code-block:: javascript
2222

source/includes/example-push-with-multiple-modifiers.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ The following :update:`$push` operation uses:
4141
}
4242
)
4343
44-
The result of the operation is keep only the three highest scoring quizzes:
44+
After the operation only the three highest scoring quizzes are in the array:
4545

4646
.. code-block:: javascript
4747

source/reference/operator/query/and.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ The query cannot use an implicit ``AND`` operation because it uses the
9090
.. seealso::
9191

9292
- :method:`~db.collection.find()`
93-
- :method:`~db.collection.update()`
93+
- :method:`~db.collection.updateMany()`
9494
- :query:`$ne`
9595
- :query:`$exists`
9696
- :update:`$set`

source/reference/operator/query/in.txt

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ $in
1616
of a field equals any value in the specified array. To specify an
1717
:query:`$in` expression, use the following prototype:
1818

19-
.. include:: /includes/fact-comparison-order.rst
20-
2119
.. code-block:: javascript
2220

2321
{ field: { $in: [<value1>, <value2>, ... <valueN> ] } }
2422

23+
.. include:: /includes/fact-comparison-order.rst
24+
2525
If the ``field`` holds an array, then the :query:`$in` operator
2626
selects the documents whose ``field`` holds an array that contains
2727
at least one element that matches a value in the specified array
@@ -36,43 +36,78 @@ $in
3636
Examples
3737
--------
3838

39+
Create the ``inventory`` collection:
40+
41+
.. code-block:: javascript
42+
43+
db.inventory.insertMany( [
44+
{ "item": "Pens", "quantity": 350, "tags": [ "school", "office" ] },
45+
{ "item": "Erasers", "quantity": 15, "tags": [ "school", "home" ] },
46+
{ "item": "Maps", "tags": [ "office", "storage" ] },
47+
{ "item": "Books", "quantity": 5, "tags": [ "school", "storage", "home" ] }
48+
] )
49+
3950
Use the ``$in`` Operator to Match Values
40-
----------------------------------------
51+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4152

4253
Consider the following example:
4354

4455
.. code-block:: javascript
4556

46-
db.inventory.find( { qty: { $in: [ 5, 15 ] } } )
57+
db.inventory.find( { quantity: { $in: [ 5, 15 ] } }, { _id: 0 } )
58+
59+
This query selects all documents in the ``inventory`` collection where
60+
the value of the ``quantity`` field is either 5 or 15.
4761

48-
This query selects all documents in the ``inventory``
49-
collection where the ``qty`` field value is either ``5`` or
50-
``15``. Although you can express this query using the
51-
:query:`$or` operator, choose the :query:`$in` operator rather
52-
than the :query:`$or` operator when performing equality checks on
53-
the same field.
62+
.. code-block:: javascript
63+
64+
{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
65+
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
66+
67+
Although you can write this query using the :query:`$or` operator,
68+
use the :query:`$in` operator rather than the :query:`$or` operator
69+
when performing equality checks on the same field.
5470

5571
Use the ``$in`` Operator to Match Values in an Array
56-
----------------------------------------------------
72+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5773

58-
The collection ``inventory`` contains documents that include the field
59-
``tags``, as in the following:
74+
The following :method:`~db.collection.updateMany()` operation sets the
75+
``exclude`` field to ``false`` when the ``tags`` array has at least one
76+
element that matches either ``"home"`` or ``"school"``.
6077

6178
.. code-block:: javascript
6279

63-
{ _id: 1, item: "abc", qty: 10, tags: [ "school", "clothing" ], sale: false }
80+
db.inventory.updateMany(
81+
{ tags: { $in: [ "home", "school" ] } },
82+
{ $set: { exclude: false } }
83+
)
6484

65-
Then, the following :method:`~db.collection.update()` operation will
66-
set the ``sale`` field value to ``true`` where the ``tags`` field holds
67-
an array with at least one element matching either ``"appliances"`` or
68-
``"school"``.
85+
Example output:
6986

7087
.. code-block:: javascript
7188

72-
db.inventory.update(
73-
{ tags: { $in: ["appliances", "school"] } },
74-
{ $set: { sale:true } }
75-
)
89+
{
90+
item: 'Pens',
91+
quantity: 350,
92+
tags: [ 'school', 'office' ],
93+
exclude: false
94+
},
95+
{
96+
item: 'Erasers',
97+
quantity: 15,
98+
tags: [ 'school', 'home' ],
99+
exclude: false
100+
},
101+
{
102+
item: 'Maps',
103+
tags: [ 'office', 'storage' ]
104+
},
105+
{
106+
item: 'Books',
107+
quantity: 5,
108+
tags: [ 'school', 'storage', 'home' ],
109+
exclude: false
110+
}
76111

77112
.. include:: /includes/extracts/additional-examples-arrays.rst
78113

@@ -99,7 +134,8 @@ the ``tags`` field holds either a string that starts with ``be`` or
99134
.. seealso::
100135

101136
- :method:`~db.collection.find()`
102-
- :method:`~db.collection.update()`
137+
- :method:`~db.collection.updateMany()`
103138
- :query:`$or`
104139
- :update:`$set`
105140
- :query:`$elemMatch`
141+

source/reference/operator/query/nin.txt

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,75 @@ $nin
1212

1313
.. query:: $nin
1414

15-
*Syntax*: ``{ field: { $nin: [ <value1>, <value2> ... <valueN> ]} }``
15+
*Syntax*: ``{ field: { $nin: [ <value1>, <value2> ... <valueN> ] } }``
1616

17-
:query:`$nin` selects the documents where:
17+
:query:`$nin` selects the documents where:
1818

19-
- the ``field`` value is not in the specified ``array`` **or**
20-
- the ``field`` does not exist.
19+
- the ``field`` value is not in the specified ``array`` **or**
20+
- the ``field`` does not exist.
2121

22-
.. include:: /includes/fact-comparison-order.rst
22+
If the ``field`` holds an array, then the :query:`$nin` operator selects
23+
the documents whose ``field`` holds an array with **no** element equal to
24+
a value in the specified array (for example, ``<value1>``,
25+
``<value2>``, and so on).
2326

24-
Consider the following query:
27+
.. include:: /includes/fact-comparison-order.rst
2528

26-
.. code-block:: javascript
29+
Examples
30+
--------
2731

28-
db.inventory.find( { qty: { $nin: [ 5, 15 ] } } )
32+
Create the ``inventory`` collection:
2933

30-
This query will select all documents in the ``inventory`` collection
31-
where the ``qty`` field value does **not** equal ``5`` nor
32-
``15``. The selected documents will include those documents that do
33-
*not* contain the ``qty`` field.
34+
.. code-block:: javascript
3435

35-
If the ``field`` holds an array, then the :query:`$nin` operator
36-
selects the documents whose ``field`` holds an array with **no**
37-
element equal to a value in the specified array (e.g. ``<value1>``,
38-
``<value2>``, etc.).
36+
db.inventory.insertMany( [
37+
{ "item": "Pens", "quantity": 350, "tags": [ "school", "office" ] },
38+
{ "item": "Erasers", "quantity": 15, "tags": [ "school", "home" ] },
39+
{ "item": "Maps", "tags": [ "office", "storage" ] },
40+
{ "item": "Books", "quantity": 5, "tags": [ "school", "storage", "home" ] }
41+
] )
3942

40-
Consider the following query:
43+
Select on Unmatching Documents
44+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4145

42-
.. code-block:: javascript
46+
The following query selects all documents from the ``inventory``
47+
collection where the ``quantity`` does **not** equal either 5 or 15.
4348

44-
db.inventory.update( { tags: { $nin: [ "appliances", "school" ] } }, { $set: { sale: false } } )
49+
The query also matches documents that do *not* have a ``quantity``
50+
field.
4551

46-
This :method:`~db.collection.update()` operation will set
47-
the ``sale`` field value in the ``inventory`` collection where the
48-
``tags`` field holds an array with **no** elements matching an
49-
element in the array ``["appliances", "school"]`` or where a
50-
document does not contain the ``tags`` field.
52+
.. code-block:: javascript
5153

52-
.. include:: /includes/extracts/nin_operators_selectivity.rst
53-
54-
.. seealso::
54+
db.inventory.find( { quantity: { $nin: [ 5, 15 ] } }, { _id: 0 } )
55+
56+
Example output:
57+
58+
.. code-block:: javascript
59+
60+
{ item: 'Pens', quantity: 350, tags: [ 'school', 'office' ] },
61+
{ item: 'Maps', tags: [ 'office', 'storage' ] }
62+
63+
Select on Elements Not in an Array
64+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
65+
66+
Set the ``exclude`` field to ``true`` for documents that don't have the
67+
``"school"`` tag.
68+
69+
.. code-block:: javascript
70+
71+
db.inventory.updateMany(
72+
{ tags: { $nin: [ "school" ] } },
73+
{ $set: { exclude: true } }
74+
)
75+
76+
:method:`~db.collection.updateMany()` also selects a document when the
77+
document does not contain the field :query:`$nin` is matching on.
78+
79+
.. include:: /includes/extracts/nin_operators_selectivity.rst
80+
81+
.. seealso::
82+
83+
- :method:`~db.collection.find()`
84+
- :method:`~db.collection.updateMany()`
85+
- :update:`$set`
5586

56-
- :method:`~db.collection.find()`
57-
- :method:`~db.collection.update()`
58-
- :update:`$set`

source/reference/operator/update.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ Update Operators
1212

1313
.. _update-operators-top-level:
1414

15-
The following modifiers are available for use in update operations;
16-
e.g. in :method:`db.collection.update()` and
15+
The following modifiers are available for use in update operations,
16+
for example, in :method:`db.collection.updateMany()` and
1717
:method:`db.collection.findAndModify()`.
1818

1919
Specify the operator expression in a document of the form:

source/reference/operator/update/addToSet.txt

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,30 @@ elements in the modified set.
4242
Missing Field
4343
~~~~~~~~~~~~~
4444

45-
If you use :update:`$addToSet` on a field that is absent in the document to
46-
update, :update:`$addToSet` creates the array field with the specified
47-
value as its element.
45+
If you use :update:`$addToSet` on a field that is absent from the
46+
document to update, :update:`$addToSet` creates the array field with
47+
the specified value as its element.
4848

4949
Field is Not an Array
5050
~~~~~~~~~~~~~~~~~~~~~~
5151

5252
If you use :update:`$addToSet` on a field that is **not** an array, the
53-
operation will fail. For example, consider a document in a collection
54-
``foo`` that contains a non-array field ``colors``.
53+
operation will fail.
54+
55+
For example, create the ``pigments`` collection:
5556

5657
.. code-block:: javascript
5758

58-
{ _id: 1, colors: "blue,green,red" }
59+
db.pigments.insertOne( { _id: 1, colors: "blue, green, red" } )
5960

60-
The following :update:`$addToSet` operation on the non-array field
61-
``colors`` fails:
61+
The ``colors`` field is not an array. The following :update:`$addToSet`
62+
operation fails:
6263

6364
.. code-block:: javascript
6465

65-
db.foo.update(
66+
db.pigments.updateOne(
6667
{ _id: 1 },
67-
{ $addToSet: { colors: "c" } }
68+
{ $addToSet: { colors: "mauve" } }
6869
)
6970

7071
Value to Add is An Array
@@ -73,29 +74,27 @@ Value to Add is An Array
7374
If the value is an array, :update:`$addToSet` appends the whole array
7475
as a *single* element.
7576

76-
Consider a document in a collection ``test`` containing an array
77-
field ``letters``:
77+
Create the ``alphabet`` collection:
7878

7979
.. code-block:: javascript
8080

81-
{ _id: 1, letters: ["a", "b"] }
81+
db.alphabet.insertOne( { _id: 1, letters: ["a", "b"] } )
8282

8383
The following operation appends the array ``[ "c", "d" ]`` to the
8484
``letters`` field:
8585

8686
.. code-block:: javascript
8787

88-
db.test.update(
88+
db.alphabet.updateOne(
8989
{ _id: 1 },
9090
{ $addToSet: { letters: [ "c", "d" ] } }
9191
)
9292

93-
The ``letters`` array now includes the ``[ "c", "d" ]`` array
94-
as an element:
93+
The array ``[ "c", "d" ]`` is added as a single element:
9594

9695
.. code-block:: javascript
9796

98-
{ _id: 1, letters: [ "a", "b", [ "c", "d" ] ] }
97+
{ _id: 1, letters: [ 'a', 'b', [ 'c', 'd' ] ] }
9998

10099
.. tip::
101100

@@ -117,11 +116,13 @@ duplicate of an existing array element.
117116
Examples
118117
--------
119118

120-
Consider a collection ``inventory`` with the following document:
119+
Create the ``inventory`` collection:
121120

122121
.. code-block:: javascript
123122

124-
{ _id: 1, item: "polarizing_filter", tags: [ "electronics", "camera" ] }
123+
db.inventory.insertOne(
124+
{ _id: 1, item: "polarizing_filter", tags: [ "electronics", "camera" ] }
125+
)
125126

126127
Add to Array
127128
~~~~~~~~~~~~
@@ -131,20 +132,20 @@ The following operation adds the element ``"accessories"`` to the
131132

132133
.. code-block:: javascript
133134

134-
db.inventory.update(
135+
db.inventory.updateOne(
135136
{ _id: 1 },
136137
{ $addToSet: { tags: "accessories" } }
137138
)
138139

139140
Value Already Exists
140141
~~~~~~~~~~~~~~~~~~~~
141142

142-
The following :update:`$addToSet` operation has no effect as
143+
The following :update:`$addToSet` operation has no effect because
143144
``"camera"`` is already an element of the ``tags`` array:
144145

145146
.. code-block:: javascript
146147

147-
db.inventory.update(
148+
db.inventory.updateOne(
148149
{ _id: 1 },
149150
{ $addToSet: { tags: "camera" } }
150151
)
@@ -163,7 +164,8 @@ field.
163164

164165
.. seealso::
165166

166-
- :method:`db.collection.update()`
167+
- :method:`db.collection.updateMany()`
167168
- :method:`db.collection.findAndModify()`
168169
- :update:`$push`
169170
- :update:`$pull`
171+

0 commit comments

Comments
 (0)