Skip to content

Commit d75e72a

Browse files
jason-price-mongodbjason-price-mongodb
andauthored
DOCS-16061 update-command-upsert-issue (#3073) (#3074)
* 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 5e3c921 commit d75e72a

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
@@ -1247,7 +1247,7 @@ collection with the following documents:
12471247
{ "grade" : 85, "mean" : 90, "std" : 4 },
12481248
{ "grade" : 85, "mean" : 85, "std" : 6 }
12491249
]
1250-
}
1250+
},
12511251
{
12521252
"_id" : 2,
12531253
"grades" : [
@@ -1302,12 +1302,12 @@ Specify ``hint`` for Update Operations
13021302

13031303
.. versionadded:: 4.2
13041304

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

13081308
.. code-block:: javascript
13091309

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

13211321
.. code-block:: javascript
13221322

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

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

13331333
.. code-block:: javascript
13341334

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

13491349
.. code-block:: javascript
13501350

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

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

0 commit comments

Comments
 (0)