@@ -740,13 +740,13 @@ with :method:`~db.collection.update()`.
740
740
:emphasize-lines: 8
741
741
742
742
db.books.update(
743
- { item: "ZZZ135" }, // Query parameter
744
- { // Replacement document
745
- item: "ZZZ135",
746
- stock: 5,
747
- tags: [ "database" ]
743
+ { item: "ZZZ135" }, // Query parameter
744
+ { $set:
745
+ {
746
+ item: "ZZZ135", stock: 5, tags: [ "database" ] // Replacement document
747
+ }
748
748
},
749
- { upsert: true } // Options
749
+ { upsert: true } // Options
750
750
)
751
751
752
752
If no document matches the ``<query>`` parameter, the update
@@ -1247,7 +1247,7 @@ collection with the following documents:
1247
1247
{ "grade" : 85, "mean" : 90, "std" : 4 },
1248
1248
{ "grade" : 85, "mean" : 85, "std" : 6 }
1249
1249
]
1250
- }
1250
+ },
1251
1251
{
1252
1252
"_id" : 2,
1253
1253
"grades" : [
@@ -1302,12 +1302,12 @@ Specify ``hint`` for Update Operations
1302
1302
1303
1303
.. versionadded:: 4.2
1304
1304
1305
- In :binary:`~bin.mongosh`, create a ``members ``
1305
+ In :binary:`~bin.mongosh`, create a ``newMembers ``
1306
1306
collection with the following documents:
1307
1307
1308
1308
.. code-block:: javascript
1309
1309
1310
- db.members .insertMany( [
1310
+ db.newMembers .insertMany( [
1311
1311
{ "_id" : 1, "member" : "abc123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
1312
1312
{ "_id" : 2, "member" : "xyz123", "status" : "A", "points" : 60, "misc1" : "reminder: ping me at 100pts", "misc2" : "Some random comment" },
1313
1313
{ "_id" : 3, "member" : "lmn123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
@@ -1320,8 +1320,8 @@ Create the following indexes on the collection:
1320
1320
1321
1321
.. code-block:: javascript
1322
1322
1323
- db.members .createIndex( { status: 1 } )
1324
- db.members .createIndex( { points: 1 } )
1323
+ db.newMembers .createIndex( { status: 1 } )
1324
+ db.newMembers .createIndex( { points: 1 } )
1325
1325
1326
1326
The following update operation explicitly :ref:`hints <update-hint>` to
1327
1327
use the index ``{status: 1 }``:
@@ -1332,7 +1332,7 @@ use the index ``{status: 1 }``:
1332
1332
1333
1333
.. code-block:: javascript
1334
1334
1335
- db.members .update(
1335
+ db.newMembers .update(
1336
1336
{ points: { $lte: 20 }, status: "P" }, // Query parameter
1337
1337
{ $set: { misc1: "Need to activate" } }, // Update document
1338
1338
{ multi: true, hint: { status: 1 } } // Options
@@ -1348,7 +1348,7 @@ To see the index used, run :dbcommand:`explain` on the operation:
1348
1348
1349
1349
.. code-block:: javascript
1350
1350
1351
- db.members .explain().update(
1351
+ db.newMembers .explain().update(
1352
1352
{ "points": { $lte: 20 }, "status": "P" },
1353
1353
{ $set: { "misc1": "Need to activate" } },
1354
1354
{ multi: true, hint: { status: 1 } }
@@ -1427,7 +1427,7 @@ option and sets ``multi`` to ``true`` to update all matching documents:
1427
1427
collation: { locale: "fr", strength: 1 },
1428
1428
multi: true
1429
1429
}
1430
- );
1430
+ )
1431
1431
1432
1432
The :ref:`write result <writeresults-update>` of the operation returns the following document, indicating that all three documents in the
1433
1433
collection were updated:
0 commit comments