@@ -722,13 +722,13 @@ with :method:`~db.collection.update()`.
722
722
:emphasize-lines: 8
723
723
724
724
db.books.update(
725
- { item: "ZZZ135" }, // Query parameter
726
- { // Replacement document
727
- item: "ZZZ135",
728
- stock: 5,
729
- tags: [ "database" ]
725
+ { item: "ZZZ135" }, // Query parameter
726
+ { $set:
727
+ {
728
+ item: "ZZZ135", stock: 5, tags: [ "database" ] // Replacement document
729
+ }
730
730
},
731
- { upsert: true } // Options
731
+ { upsert: true } // Options
732
732
)
733
733
734
734
If no document matches the ``<query>`` parameter, the update
@@ -1230,7 +1230,7 @@ collection with the following documents:
1230
1230
{ "grade" : 85, "mean" : 90, "std" : 4 },
1231
1231
{ "grade" : 85, "mean" : 85, "std" : 6 }
1232
1232
]
1233
- }
1233
+ },
1234
1234
{
1235
1235
"_id" : 2,
1236
1236
"grades" : [
@@ -1285,12 +1285,12 @@ Specify ``hint`` for Update Operations
1285
1285
1286
1286
.. versionadded:: 4.2
1287
1287
1288
- From the :binary:`~bin.mongo` shell , create a ``members ``
1288
+ In :binary:`~bin.mongosh` , create a ``newMembers ``
1289
1289
collection with the following documents:
1290
1290
1291
1291
.. code-block:: javascript
1292
1292
1293
- db.members .insertMany([
1293
+ db.newMembers .insertMany( [
1294
1294
{ "_id" : 1, "member" : "abc123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
1295
1295
{ "_id" : 2, "member" : "xyz123", "status" : "A", "points" : 60, "misc1" : "reminder: ping me at 100pts", "misc2" : "Some random comment" },
1296
1296
{ "_id" : 3, "member" : "lmn123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
@@ -1303,8 +1303,8 @@ Create the following indexes on the collection:
1303
1303
1304
1304
.. code-block:: javascript
1305
1305
1306
- db.members .createIndex( { status: 1 } )
1307
- db.members .createIndex( { points: 1 } )
1306
+ db.newMembers .createIndex( { status: 1 } )
1307
+ db.newMembers .createIndex( { points: 1 } )
1308
1308
1309
1309
The following update operation explicitly :ref:`hints <update-hint>` to
1310
1310
use the index ``{status: 1 }``:
@@ -1315,7 +1315,7 @@ use the index ``{status: 1 }``:
1315
1315
1316
1316
.. code-block:: javascript
1317
1317
1318
- db.members .update(
1318
+ db.newMembers .update(
1319
1319
{ points: { $lte: 20 }, status: "P" }, // Query parameter
1320
1320
{ $set: { misc1: "Need to activate" } }, // Update document
1321
1321
{ multi: true, hint: { status: 1 } } // Options
@@ -1331,7 +1331,7 @@ To see the index used, run :dbcommand:`explain` on the operation:
1331
1331
1332
1332
.. code-block:: javascript
1333
1333
1334
- db.members .explain().update(
1334
+ db.newMembers .explain().update(
1335
1335
{ "points": { $lte: 20 }, "status": "P" },
1336
1336
{ $set: { "misc1": "Need to activate" } },
1337
1337
{ multi: true, hint: { status: 1 } }
@@ -1393,7 +1393,7 @@ option and sets ``multi`` to ``true`` to update all matching documents:
1393
1393
collation: { locale: "fr", strength: 1 },
1394
1394
multi: true
1395
1395
}
1396
- );
1396
+ )
1397
1397
1398
1398
The :ref:`write result <writeresults-update>` of the operation returns the following document, indicating that all three documents in the
1399
1399
collection were updated:
0 commit comments