File tree Expand file tree Collapse file tree 3 files changed +62
-6
lines changed
code-snippets/usage-examples Expand file tree Collapse file tree 3 files changed +62
-6
lines changed Original file line number Diff line number Diff line change @@ -14,16 +14,15 @@ async function run() {
14
14
const movies = database . collection ( "movies" ) ;
15
15
16
16
// create a filter for a movie to update
17
- const filter = { title : "Blacksmith Scene " } ;
17
+ const filter = { title : "Random Harvest " } ;
18
18
19
19
// this option instructs the method to create a document if no documents match the filter
20
20
const options = { upsert : true } ;
21
21
22
22
// create a document that sets the plot of the movie
23
23
const updateDoc = {
24
24
$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 ( ) } `
27
26
} ,
28
27
} ;
29
28
Original file line number Diff line number Diff line change
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 ) ;
Original file line number Diff line number Diff line change @@ -52,6 +52,26 @@ see the :manual:`MongoDB update operator reference documentation
52
52
53
53
.. include:: /includes/connect-guide-note.rst
54
54
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
+
You can’t perform that action at this time.
0 commit comments