@@ -1042,31 +1042,31 @@ field values or updating one field using the value of another field(s).
10421042Modify 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
10561056update 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
10941094Second 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
10971098After 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 ``
13061307collection 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
13261326The 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+
13411341The 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
13571357The :method:`db.collection.explain().update() <db.collection.explain>`
0 commit comments