Skip to content

Commit 78716cd

Browse files
DOCS-16061 update-command-upsert-issue (#3077)
1 parent 9b084d2 commit 78716cd

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

source/includes/fact-upsert-id.rst

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ result in an error when constructing the document to insert.
1717

1818
.. code-block:: javascript
1919
20-
db.collection.update( { "_id.name": "Robert Frost", "_id.uid": 0 },
21-
{ "categories": ["poet", "playwright"] },
22-
{ upsert: true } )
20+
db.collection.update(
21+
{ "_id.name": "Robert Frost", "_id.uid": 0 }, // Query parameter
22+
{ $set:
23+
{
24+
"categories": [ "poet", "playwright" ] // Replacement document
25+
}
26+
},
27+
{ upsert: true } // Options
28+
)

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -722,13 +722,13 @@ with :method:`~db.collection.update()`.
722722
:emphasize-lines: 8
723723

724724
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+
}
730730
},
731-
{ upsert: true } // Options
731+
{ upsert: true } // Options
732732
)
733733

734734
If no document matches the ``<query>`` parameter, the update
@@ -1230,7 +1230,7 @@ collection with the following documents:
12301230
{ "grade" : 85, "mean" : 90, "std" : 4 },
12311231
{ "grade" : 85, "mean" : 85, "std" : 6 }
12321232
]
1233-
}
1233+
},
12341234
{
12351235
"_id" : 2,
12361236
"grades" : [
@@ -1285,12 +1285,12 @@ Specify ``hint`` for Update Operations
12851285

12861286
.. versionadded:: 4.2
12871287

1288-
From the :binary:`~bin.mongo` shell, create a ``members``
1288+
In :binary:`~bin.mongosh`, create a ``newMembers``
12891289
collection with the following documents:
12901290

12911291
.. code-block:: javascript
12921292

1293-
db.members.insertMany([
1293+
db.newMembers.insertMany( [
12941294
{ "_id" : 1, "member" : "abc123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
12951295
{ "_id" : 2, "member" : "xyz123", "status" : "A", "points" : 60, "misc1" : "reminder: ping me at 100pts", "misc2" : "Some random comment" },
12961296
{ "_id" : 3, "member" : "lmn123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
@@ -1303,8 +1303,8 @@ Create the following indexes on the collection:
13031303

13041304
.. code-block:: javascript
13051305

1306-
db.members.createIndex( { status: 1 } )
1307-
db.members.createIndex( { points: 1 } )
1306+
db.newMembers.createIndex( { status: 1 } )
1307+
db.newMembers.createIndex( { points: 1 } )
13081308

13091309
The following update operation explicitly :ref:`hints <update-hint>` to
13101310
use the index ``{status: 1 }``:
@@ -1315,7 +1315,7 @@ use the index ``{status: 1 }``:
13151315

13161316
.. code-block:: javascript
13171317

1318-
db.members.update(
1318+
db.newMembers.update(
13191319
{ points: { $lte: 20 }, status: "P" }, // Query parameter
13201320
{ $set: { misc1: "Need to activate" } }, // Update document
13211321
{ multi: true, hint: { status: 1 } } // Options
@@ -1331,7 +1331,7 @@ To see the index used, run :dbcommand:`explain` on the operation:
13311331

13321332
.. code-block:: javascript
13331333

1334-
db.members.explain().update(
1334+
db.newMembers.explain().update(
13351335
{ "points": { $lte: 20 }, "status": "P" },
13361336
{ $set: { "misc1": "Need to activate" } },
13371337
{ multi: true, hint: { status: 1 } }
@@ -1393,7 +1393,7 @@ option and sets ``multi`` to ``true`` to update all matching documents:
13931393
collation: { locale: "fr", strength: 1 },
13941394
multi: true
13951395
}
1396-
);
1396+
)
13971397

13981398
The :ref:`write result <writeresults-update>` of the operation returns the following document, indicating that all three documents in the
13991399
collection were updated:

0 commit comments

Comments
 (0)