Skip to content

Commit 4db63db

Browse files
(DOCSP-17936) Update One TS (#234)
1 parent 5203c27 commit 4db63db

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

source/code-snippets/usage-examples/updateOne.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@ async function run() {
1414
const movies = database.collection("movies");
1515

1616
// create a filter for a movie to update
17-
const filter = { title: "Blacksmith Scene" };
17+
const filter = { title: "Random Harvest" };
1818

1919
// this option instructs the method to create a document if no documents match the filter
2020
const options = { upsert: true };
2121

2222
// create a document that sets the plot of the movie
2323
const updateDoc = {
2424
$set: {
25-
plot:
26-
"Blacksmith Scene is a silent film directed by William K.L. Dickson.",
25+
plot: `A harvest of random numbers, such as: ${Math.random()}`
2726
},
2827
};
2928

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { MongoClient } from "mongodb";
2+
3+
// Replace the uri string with your MongoDB deployment's connection string.
4+
const uri =
5+
"mongodb+srv://<user>:<password>@<cluster-url>?writeConcern=majority";
6+
7+
const client = new MongoClient(uri);
8+
9+
interface Movies {
10+
plot: string;
11+
title: string;
12+
}
13+
14+
async function run() {
15+
try {
16+
await client.connect();
17+
18+
const database = client.db("sample_mflix");
19+
const movies = database.collection<Movies>("movies");
20+
21+
const result = await movies.updateOne(
22+
{ title: "Random Harvest" },
23+
{
24+
$set: {
25+
plot: `A harvest of random numbers, such as: ${Math.random()}`,
26+
},
27+
},
28+
{ upsert: true }
29+
);
30+
console.log(
31+
`${result.matchedCount} document(s) matched the filter, updated ${result.modifiedCount} document(s)`
32+
);
33+
} finally {
34+
await client.close();
35+
}
36+
}
37+
run().catch(console.dir);

source/usage-examples/updateOne.txt

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,26 @@ see the :manual:`MongoDB update operator reference documentation
5252

5353
.. include:: /includes/connect-guide-note.rst
5454

55-
.. literalinclude:: /code-snippets/usage-examples/updateOne.js
56-
:language: javascript
57-
:linenos:
55+
.. tabs::
56+
57+
.. tab:: JavaScript
58+
:tabid: javascript
59+
60+
.. literalinclude:: /code-snippets/usage-examples/updateOne.js
61+
:language: javascript
62+
:linenos:
63+
64+
.. tab:: TypeScript
65+
:tabid: typescript
66+
67+
.. literalinclude:: /code-snippets/usage-examples/updateOne.ts
68+
:language: typescript
69+
:linenos:
70+
71+
If you run the example above, you should see output that resembles the following:
72+
73+
.. code-block:: none
74+
:copyable: false
75+
76+
1 document(s) matched the filter, updated 1 document(s)
77+

0 commit comments

Comments
 (0)