Skip to content

Commit b608e7d

Browse files
DOCSP-24202 backport 6.3 (#4325)
* DOCSP-24202 Improved members example (#3982) * DOCSP-24202 updated members examples to be more specific and understandable * fixed typos and updated several incorrrect examples * fixed copy * fix merge conflict --------- Co-authored-by: jmd-mongo <[email protected]> * deleted section with merge errors --------- Co-authored-by: jmd-mongo <[email protected]>
1 parent 83993a6 commit b608e7d

File tree

4 files changed

+134
-115
lines changed

4 files changed

+134
-115
lines changed

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -278,35 +278,34 @@ Specify ``hint`` for Delete Operations
278278

279279
.. versionadded:: 4.4
280280

281-
In :binary:`~bin.mongosh`, create a ``members`` collection
281+
In :binary:`~bin.mongosh`, create a ``students`` collection
282282
with the following documents:
283283

284284
.. code-block:: javascript
285285

286286
db.members.insertMany([
287-
{ "_id" : 1, "member" : "abc123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
288-
{ "_id" : 2, "member" : "xyz123", "status" : "A", "points" : 60, "misc1" : "reminder: ping me at 100pts", "misc2" : "Some random comment" },
289-
{ "_id" : 3, "member" : "lmn123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
290-
{ "_id" : 4, "member" : "pqr123", "status" : "D", "points" : 20, "misc1" : "Deactivated", "misc2" : null },
291-
{ "_id" : 5, "member" : "ijk123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
292-
{ "_id" : 6, "member" : "cde123", "status" : "A", "points" : 86, "misc1" : "reminder: ping me at 100pts", "misc2" : "Some random comment" }
287+
{ "_id" : 1, "student" : "Richard", "grade" : "F", "points" : 0 },
288+
{ "_id" : 2, "student" : "Jane", "grade" : "A", "points" : 60 },
289+
{ "_id" : 3, "student" : "Adam", "grade" : "F", "points" : 0 },
290+
{ "_id" : 4, "student" : "Ronan", "grade" : "D", "points" : 20 },
291+
{ "_id" : 5, "student" : "Noah", "grade" : "F", "points" : 0 },
292+
{ "_id" : 6, "student" : "Henry", "grade" : "A", "points" : 86 }
293293
])
294294

295-
Create the following indexes on the collection:
295+
Create the following index on the collection:
296296

297297
.. code-block:: javascript
298298

299-
db.members.createIndex( { status: 1 } )
300-
db.members.createIndex( { points: 1 } )
299+
db.members.createIndex( { grade: 1 } )
301300

302301
The following delete operation explicitly hints to use the index
303-
``{ status: 1 }``:
302+
``{ grade: 1 }``:
304303

305304
.. code-block:: javascript
306305

307306
db.members.deleteOne(
308-
{ "points": { $lte: 20 }, "status": "P" },
309-
{ hint: { status: 1 } }
307+
{ "points": { $lte: 20 }, "grade": "F" },
308+
{ hint: { grade: 1 } }
310309
)
311310

312311
.. note::

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

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,31 +1042,31 @@ field values or updating one field using the value of another field(s).
10421042
Modify a Field Using the Values of the Other Fields in the Document
10431043
```````````````````````````````````````````````````````````````````
10441044

1045-
Create a ``members`` collection with the following documents:
1045+
Create a ``students`` collection with the following documents:
10461046

10471047
.. code-block:: javascript
10481048

1049-
db.members.insertMany( [
1050-
{ "_id" : 1, "member" : "abc123", "status" : "A", "points" : 2, "misc1" : "note to self: confirm status", "misc2" : "Need to activate", "lastUpdate" : ISODate("2019-01-01T00:00:00Z") },
1051-
{ "_id" : 2, "member" : "xyz123", "status" : "A", "points" : 60, "misc1" : "reminder: ping me at 100pts", "misc2" : "Some random comment", "lastUpdate" : ISODate("2019-01-01T00:00:00Z") }
1049+
db.students.insertMany( [
1050+
{ "_id" : 1, "student" : "Skye", "points" : 75, "commentsSemester1" : "great at math", "commentsSemester2" : "loses temper", "lastUpdate" : ISODate("2019-01-01T00:00:00Z") },
1051+
{ "_id" : 2, "students" : "Elizabeth", "points" : 60, "commentsSemester1" : "well behaved", "commentsSemester2" : "needs improvement", "lastUpdate" : ISODate("2019-01-01T00:00:00Z") }
10521052
] )
10531053

1054-
Assume that instead of separate ``misc1`` and ``misc2`` fields, you
1055-
want to gather these into a new ``comments`` field. The following
1054+
Assume that instead of separate ``commentsSemester1`` and ``commentsSemester2``
1055+
fields, you want to gather these into a new ``comments`` field. The following
10561056
update operation uses an aggregation pipeline to:
10571057

10581058
- add the new ``comments`` field and set the ``lastUpdate`` field.
10591059

1060-
- remove the ``misc1`` and ``misc2`` fields for all documents in the
1061-
collection.
1060+
- remove the ``commentsSemester1`` and ``commentsSemester2`` fields for all
1061+
documents in the collection.
10621062

10631063
.. code-block:: javascript
10641064

10651065
db.members.update(
10661066
{ },
10671067
[
1068-
{ $set: { status: "Modified", comments: [ "$misc1", "$misc2" ], lastUpdate: "$$NOW" } },
1069-
{ $unset: [ "misc1", "misc2" ] }
1068+
{ $set: { comments: [ "$commentsSemester1", "$commentsSemester2" ], lastUpdate: "$$NOW" } },
1069+
{ $unset: [ "commentsSemester1", "commentsSemester2" ] }
10701070
],
10711071
{ multi: true }
10721072
)
@@ -1082,7 +1082,7 @@ First Stage
10821082
The :pipeline:`$set` stage:
10831083

10841084
- creates a new array field ``comments`` whose elements are the current
1085-
content of the ``misc1`` and ``misc2`` fields and
1085+
content of the ``commentsSemester1`` and ``commentsSemester2`` fields and
10861086

10871087
- sets the field ``lastUpdate`` to the value of the aggregation
10881088
variable :variable:`NOW`. The aggregation variable
@@ -1092,14 +1092,15 @@ First Stage
10921092
and enclose in quotes.
10931093

10941094
Second Stage
1095-
The :pipeline:`$unset` stage removes the ``misc1`` and ``misc2`` fields.
1095+
The :pipeline:`$unset` stage removes the ``commentsSemester1`` and
1096+
``commentsSemester2`` fields.
10961097

10971098
After the command, the collection contains the following documents:
10981099

10991100
.. code-block:: javascript
11001101

1101-
{ "_id" : 1, "member" : "abc123", "status" : "Modified", "points" : 2, "lastUpdate" : ISODate("2020-01-23T05:11:45.784Z"), "comments" : [ "note to self: confirm status", "Need to activate" ] }
1102-
{ "_id" : 2, "member" : "xyz123", "status" : "Modified", "points" : 60, "lastUpdate" : ISODate("2020-01-23T05:11:45.784Z"), "comments" : [ "reminder: ping me at 100pts", "Some random comment" ] }
1102+
{ "_id" : 1, "student" : "Skye", "status" : "Modified", "points" : 75, "lastUpdate" : ISODate("2020-01-23T05:11:45.784Z"), "comments" : [ "great at math", "loses temper" ] }
1103+
{ "_id" : 2, "student" : "Elizabeth", "status" : "Modified", "points" : 60, "lastUpdate" : ISODate("2020-01-23T05:11:45.784Z"), "comments" : [ "well behaved", "needs improvement" ] }
11031104

11041105
.. seealso::
11051106

@@ -1302,42 +1303,41 @@ Specify ``hint`` for Update Operations
13021303

13031304
.. versionadded:: 4.2
13041305

1305-
In :binary:`~bin.mongosh`, create a ``newMembers``
1306+
In :binary:`~bin.mongosh`, create a ``newStudents``
13061307
collection with the following documents:
13071308

13081309
.. code-block:: javascript
13091310

1310-
db.newMembers.insertMany( [
1311-
{ "_id" : 1, "member" : "abc123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
1312-
{ "_id" : 2, "member" : "xyz123", "status" : "A", "points" : 60, "misc1" : "reminder: ping me at 100pts", "misc2" : "Some random comment" },
1313-
{ "_id" : 3, "member" : "lmn123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
1314-
{ "_id" : 4, "member" : "pqr123", "status" : "D", "points" : 20, "misc1" : "Deactivated", "misc2" : null },
1315-
{ "_id" : 5, "member" : "ijk123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
1316-
{ "_id" : 6, "member" : "cde123", "status" : "A", "points" : 86, "misc1" : "reminder: ping me at 100pts", "misc2" : "Some random comment" }
1311+
db.newStudents.insertMany( [
1312+
{ "_id" : 1, "student" : "Richard", "grade" : "F", "points" : 0, "comments1" : null, "comments2" : null },
1313+
{ "_id" : 2, "student" : "Jane", "grade" : "A", "points" : 60, "comments1" : "well behaved", "comments2" : "fantastic student" },
1314+
{ "_id" : 3, "student" : "Ronan", "grade" : "F", "points" : 0, "comments1" : null, "comments2" : null },
1315+
{ "_id" : 4, "student" : "Noah", "grade" : "D", "points" : 20, "comments1" : "needs improvement", "comments2" : null },
1316+
{ "_id" : 5, "student" : "Adam", "grade" : "F", "points" : 0, "comments1" : null, "comments2" : null },
1317+
{ "_id" : 6, "student" : "Henry", "grade" : "A", "points" : 86, "comments1" : "fantastic student", "comments2" : "well behaved" }
13171318
] )
13181319

1319-
Create the following indexes on the collection:
1320+
Create the following index on the collection:
13201321

13211322
.. code-block:: javascript
13221323

1323-
db.newMembers.createIndex( { status: 1 } )
1324-
db.newMembers.createIndex( { points: 1 } )
1324+
db.newStudents.createIndex( { grade: 1 } )
13251325

13261326
The following update operation explicitly :ref:`hints <update-hint>` to
1327-
use the index ``{status: 1 }``:
1328-
1329-
.. note::
1330-
1331-
If you specify an index that does not exist, the operation errors.
1327+
use the index ``{grade: 1 }``:
13321328

13331329
.. code-block:: javascript
13341330

1335-
db.newMembers.update(
1336-
{ points: { $lte: 20 }, status: "P" }, // Query parameter
1337-
{ $set: { misc1: "Need to activate" } }, // Update document
1338-
{ multi: true, hint: { status: 1 } } // Options
1331+
db.newStudents.update(
1332+
{ points: { $lte: 20 }, grade: "F" }, // Query parameter
1333+
{ $set: { comments1: "failed class" } }, // Update document
1334+
{ multi: true, hint: { grade: 1 } } // Options
13391335
)
13401336

1337+
.. note::
1338+
1339+
If you specify an index that does not exist, the operation errors.
1340+
13411341
The update command returns the following:
13421342

13431343
.. code-block:: javascript
@@ -1348,10 +1348,10 @@ To see the index used, run :dbcommand:`explain` on the operation:
13481348

13491349
.. code-block:: javascript
13501350

1351-
db.newMembers.explain().update(
1352-
{ "points": { $lte: 20 }, "status": "P" },
1353-
{ $set: { "misc1": "Need to activate" } },
1354-
{ multi: true, hint: { status: 1 } }
1351+
db.newStudents.explain().update(
1352+
{ "points": { $lte: 20 }, "grade": "F" },
1353+
{ $set: { "comments1": "failed class" } },
1354+
{ multi: true, hint: { grade: 1 } }
13551355
)
13561356

13571357
The :method:`db.collection.explain().update() <db.collection.explain>`

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

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -406,31 +406,31 @@ Example 1: Update with Aggregation Pipeline Using Existing Fields
406406
The following examples uses the aggregation pipeline to modify a field
407407
using the values of the other fields in the document.
408408

409-
Create a ``members`` collection with the following documents:
409+
Create a ``students`` collection with the following documents:
410410

411411
.. code-block:: javascript
412412

413-
db.members.insertMany( [
414-
{ "_id" : 1, "member" : "abc123", "status" : "A", "points" : 2, "misc1" : "note to self: confirm status", "misc2" : "Need to activate", "lastUpdate" : ISODate("2019-01-01T00:00:00Z") },
415-
{ "_id" : 2, "member" : "xyz123", "status" : "A", "points" : 60, "misc1" : "reminder: ping me at 100pts", "misc2" : "Some random comment", "lastUpdate" : ISODate("2019-01-01T00:00:00Z") }
413+
db.students.insertMany( [
414+
{ "_id" : 1, "student" : "Skye", "points" : 75, "commentsSemester1" : "great at math", "commentsSemester2" : "loses temper", "lastUpdate" : ISODate("2019-01-01T00:00:00Z") },
415+
{ "_id" : 2, "students" : "Elizabeth", "points" : 60, "commentsSemester1" : "well behaved", "commentsSemester2" : "needs improvement", "lastUpdate" : ISODate("2019-01-01T00:00:00Z") }
416416
] )
417417

418-
Assume that instead of separate ``misc1`` and ``misc2`` fields, you
419-
want to gather these into a new ``comments`` field. The following
418+
Assume that instead of separate ``commentsSemester1`` and ``commentsSemester2``
419+
fields, you want to gather these into a new ``comments`` field. The following
420420
update operation uses an aggregation pipeline to:
421421

422422
- add the new ``comments`` field and set the ``lastUpdate`` field.
423423

424-
- remove the ``misc1`` and ``misc2`` fields for all documents in the
425-
collection.
424+
- remove the ``commentsSemester1`` and ``commentsSemester2`` fields for all
425+
documents in the collection.
426426

427427
.. code-block:: javascript
428428

429-
db.members.updateMany(
429+
db.students.updateMany(
430430
{ },
431431
[
432-
{ $set: { status: "Modified", comments: [ "$misc1", "$misc2" ], lastUpdate: "$$NOW" } },
433-
{ $unset: [ "misc1", "misc2" ] }
432+
{ $set: { comments: [ "$commentsSemester1", "$commentsSemester2" ], lastUpdate: "$$NOW" } },
433+
{ $unset: [ "commentsSemester1", "commentsSemester2" ] }
434434
]
435435
)
436436

@@ -444,7 +444,7 @@ First Stage
444444
The :pipeline:`$set` stage:
445445

446446
- creates a new array field ``comments`` whose elements are the current
447-
content of the ``misc1`` and ``misc2`` fields and
447+
content of the ``commentsSemester1`` and ``commentsSemester2`` fields and
448448

449449
- sets the field ``lastUpdate`` to the value of the aggregation
450450
variable :variable:`NOW`. The aggregation variable
@@ -454,14 +454,15 @@ First Stage
454454
and enclose in quotes.
455455

456456
Second Stage
457-
The :pipeline:`$unset` stage removes the ``misc1`` and ``misc2`` fields.
457+
The :pipeline:`$unset` stage removes the ``commentsSemester1`` and
458+
``commentsSemester2`` fields.
458459

459460
After the command, the collection contains the following documents:
460461

461462
.. code-block:: javascript
462463

463-
{ "_id" : 1, "member" : "abc123", "status" : "Modified", "points" : 2, "lastUpdate" : ISODate("2020-01-23T05:50:49.247Z"), "comments" : [ "note to self: confirm status", "Need to activate" ] }
464-
{ "_id" : 2, "member" : "xyz123", "status" : "Modified", "points" : 60, "lastUpdate" : ISODate("2020-01-23T05:50:49.247Z"), "comments" : [ "reminder: ping me at 100pts", "Some random comment" ] }
464+
{ "_id" : 1, "student" : "Skye", "status" : "Modified", "points" : 75, "lastUpdate" : ISODate("2020-01-23T05:11:45.784Z"), "comments" : [ "great at math", "loses temper" ] }
465+
{ "_id" : 2, "student" : "Elizabeth", "status" : "Modified", "points" : 60, "lastUpdate" : ISODate("2020-01-23T05:11:45.784Z"), "comments" : [ "well behaved", "needs improvement" ] }
465466

466467
Example 2: Update with Aggregation Pipeline Using Existing Fields Conditionally
467468
```````````````````````````````````````````````````````````````````````````````
@@ -775,39 +776,38 @@ Specify ``hint`` for Update Operations
775776

776777
.. versionadded:: 4.2.1
777778

778-
Create a sample ``members`` collection with the following documents:
779+
Create a sample ``students`` collection with the following documents:
779780

780781
.. code-block:: javascript
781782

782-
db.members.insertMany( [
783-
{ "_id" : 1, "member" : "abc123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
784-
{ "_id" : 2, "member" : "xyz123", "status" : "A", "points" : 60, "misc1" : "reminder: ping me at 100pts", "misc2" : "Some random comment" },
785-
{ "_id" : 3, "member" : "lmn123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
786-
{ "_id" : 4, "member" : "pqr123", "status" : "D", "points" : 20, "misc1" : "Deactivated", "misc2" : null },
787-
{ "_id" : 5, "member" : "ijk123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
788-
{ "_id" : 6, "member" : "cde123", "status" : "A", "points" : 86, "misc1" : "reminder: ping me at 100pts", "misc2" : "Some random comment" }
783+
db.students.insertMany( [
784+
{ "_id" : 1, "student" : "Richard", "grade" : "F", "points" : 0, "comments1" : null, "comments2" : null },
785+
{ "_id" : 2, "student" : "Jane", "grade" : "A", "points" : 60, "comments1" : "well behaved", "comments2" : "fantastic student" },
786+
{ "_id" : 3, "student" : "Ronan", "grade" : "F", "points" : 0, "comments1" : null, "comments2" : null },
787+
{ "_id" : 4, "student" : "Noah", "grade" : "D", "points" : 20, "comments1" : "needs improvement", "comments2" : null },
788+
{ "_id" : 5, "student" : "Adam", "grade" : "F", "points" : 0, "comments1" : null, "comments2" : null },
789+
{ "_id" : 6, "student" : "Henry", "grade" : "A", "points" : 86, "comments1" : "fantastic student", "comments2" : "well behaved" }
789790
] )
790791

791792
Create the following indexes on the collection:
792793

793794
.. code-block:: javascript
794795

795-
db.members.createIndex( { status: 1 } )
796-
db.members.createIndex( { points: 1 } )
796+
db.students.createIndex( { grade: 1 } )
797797

798798
The following update operation explicitly hints to use the index ``{
799-
status: 1 }``:
799+
grade: 1 }``:
800800

801801
.. note::
802802

803803
If you specify an index that does not exist, the operation errors.
804804

805805
.. code-block:: javascript
806806

807-
db.members.updateMany(
808-
{ "points": { $lte: 20 }, "status": "P" },
809-
{ $set: { "misc1": "Need to activate" } },
810-
{ hint: { status: 1 } }
807+
db.students.updateMany(
808+
{ "points": { $lte: 20 }, "grade": "F" },
809+
{ $set: { "comments1": "failed class" } },
810+
{ hint: { grade: 1 } }
811811
)
812812

813813
The update command returns the following:
@@ -816,8 +816,12 @@ The update command returns the following:
816816

817817
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
818818

819-
To view the indexes used, you can use the :pipeline:`$indexStats` pipeline:
819+
To see the index used, run :dbcommand:`explain` on the operation:
820820

821821
.. code-block:: javascript
822822

823-
db.members.aggregate( [ { $indexStats: { } }, { $sort: { name: 1 } } ] )
823+
db.students.explain().updateMany(
824+
{ "points": { $lte: 20 }, "grade": "F" },
825+
{ $set: { "comments1": "failed class" } },
826+
{ multi: true, hint: { grade: 1 } }
827+
)

0 commit comments

Comments
 (0)