Skip to content

Commit 997a4c1

Browse files
jason-price-mongodbjason-price-mongodb
andauthored
DOCS-16061 update-command-upsert-issue (#3073) (#3075)
* DOCS-16061-update-command-upsert-issue * DOCS-16061-update-command-upsert-issue * DOCS-16061-update-command-upsert-issue --------- Co-authored-by: jason-price-mongodb <[email protected]>
1 parent b327bae commit 997a4c1

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
@@ -740,13 +740,13 @@ with :method:`~db.collection.update()`.
740740
:emphasize-lines: 8
741741

742742
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+
}
748748
},
749-
{ upsert: true } // Options
749+
{ upsert: true } // Options
750750
)
751751

752752
If no document matches the ``<query>`` parameter, the update
@@ -1248,7 +1248,7 @@ collection with the following documents:
12481248
{ "grade" : 85, "mean" : 90, "std" : 4 },
12491249
{ "grade" : 85, "mean" : 85, "std" : 6 }
12501250
]
1251-
}
1251+
},
12521252
{
12531253
"_id" : 2,
12541254
"grades" : [
@@ -1303,12 +1303,12 @@ Specify ``hint`` for Update Operations
13031303

13041304
.. versionadded:: 4.2
13051305

1306-
In :binary:`~bin.mongosh`, create a ``members``
1306+
In :binary:`~bin.mongosh`, create a ``newMembers``
13071307
collection with the following documents:
13081308

13091309
.. code-block:: javascript
13101310

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

13221322
.. code-block:: javascript
13231323

1324-
db.members.createIndex( { status: 1 } )
1325-
db.members.createIndex( { points: 1 } )
1324+
db.newMembers.createIndex( { status: 1 } )
1325+
db.newMembers.createIndex( { points: 1 } )
13261326

13271327
The following update operation explicitly :ref:`hints <update-hint>` to
13281328
use the index ``{status: 1 }``:
@@ -1333,7 +1333,7 @@ use the index ``{status: 1 }``:
13331333

13341334
.. code-block:: javascript
13351335

1336-
db.members.update(
1336+
db.newMembers.update(
13371337
{ points: { $lte: 20 }, status: "P" }, // Query parameter
13381338
{ $set: { misc1: "Need to activate" } }, // Update document
13391339
{ multi: true, hint: { status: 1 } } // Options
@@ -1349,7 +1349,7 @@ To see the index used, run :dbcommand:`explain` on the operation:
13491349

13501350
.. code-block:: javascript
13511351

1352-
db.members.explain().update(
1352+
db.newMembers.explain().update(
13531353
{ "points": { $lte: 20 }, "status": "P" },
13541354
{ $set: { "misc1": "Need to activate" } },
13551355
{ multi: true, hint: { status: 1 } }
@@ -1428,7 +1428,7 @@ option and sets ``multi`` to ``true`` to update all matching documents:
14281428
collation: { locale: "fr", strength: 1 },
14291429
multi: true
14301430
}
1431-
);
1431+
)
14321432

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

0 commit comments

Comments
 (0)