@@ -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
@@ -1248,7 +1248,7 @@ collection with the following documents:
1248
1248
{ "grade" : 85, "mean" : 90, "std" : 4 },
1249
1249
{ "grade" : 85, "mean" : 85, "std" : 6 }
1250
1250
]
1251
- }
1251
+ },
1252
1252
{
1253
1253
"_id" : 2,
1254
1254
"grades" : [
@@ -1303,12 +1303,12 @@ Specify ``hint`` for Update Operations
1303
1303
1304
1304
.. versionadded:: 4.2
1305
1305
1306
- In :binary:`~bin.mongosh`, create a ``members ``
1306
+ In :binary:`~bin.mongosh`, create a ``newMembers ``
1307
1307
collection with the following documents:
1308
1308
1309
1309
.. code-block:: javascript
1310
1310
1311
- db.members .insertMany( [
1311
+ db.newMembers .insertMany( [
1312
1312
{ "_id" : 1, "member" : "abc123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
1313
1313
{ "_id" : 2, "member" : "xyz123", "status" : "A", "points" : 60, "misc1" : "reminder: ping me at 100pts", "misc2" : "Some random comment" },
1314
1314
{ "_id" : 3, "member" : "lmn123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
@@ -1321,8 +1321,8 @@ Create the following indexes on the collection:
1321
1321
1322
1322
.. code-block:: javascript
1323
1323
1324
- db.members .createIndex( { status: 1 } )
1325
- db.members .createIndex( { points: 1 } )
1324
+ db.newMembers .createIndex( { status: 1 } )
1325
+ db.newMembers .createIndex( { points: 1 } )
1326
1326
1327
1327
The following update operation explicitly :ref:`hints <update-hint>` to
1328
1328
use the index ``{status: 1 }``:
@@ -1333,7 +1333,7 @@ use the index ``{status: 1 }``:
1333
1333
1334
1334
.. code-block:: javascript
1335
1335
1336
- db.members .update(
1336
+ db.newMembers .update(
1337
1337
{ points: { $lte: 20 }, status: "P" }, // Query parameter
1338
1338
{ $set: { misc1: "Need to activate" } }, // Update document
1339
1339
{ multi: true, hint: { status: 1 } } // Options
@@ -1349,7 +1349,7 @@ To see the index used, run :dbcommand:`explain` on the operation:
1349
1349
1350
1350
.. code-block:: javascript
1351
1351
1352
- db.members .explain().update(
1352
+ db.newMembers .explain().update(
1353
1353
{ "points": { $lte: 20 }, "status": "P" },
1354
1354
{ $set: { "misc1": "Need to activate" } },
1355
1355
{ multi: true, hint: { status: 1 } }
@@ -1428,7 +1428,7 @@ option and sets ``multi`` to ``true`` to update all matching documents:
1428
1428
collation: { locale: "fr", strength: 1 },
1429
1429
multi: true
1430
1430
}
1431
- );
1431
+ )
1432
1432
1433
1433
The :ref:`write result <writeresults-update>` of the operation returns the following document, indicating that all three documents in the
1434
1434
collection were updated:
0 commit comments