File tree Expand file tree Collapse file tree 3 files changed +69
-6
lines changed
code-snippets/usage-examples Expand file tree Collapse file tree 3 files changed +69
-6
lines changed Original file line number Diff line number Diff line change @@ -18,12 +18,14 @@ async function run() {
1818
1919 // increment every document matching the filter with 2 more comments
2020 const updateDoc = {
21- $inc : {
22- num_mflix_comments : 2 ,
21+ $set : {
22+ random_review : `After viewing I am ${
23+ 100 * Math . random ( )
24+ } % more satisfied with life.`,
2325 } ,
2426 } ;
2527 const result = await movies . updateMany ( filter , updateDoc ) ;
26- console . log ( result ) ;
28+ console . log ( `Updated ${ result . modifiedCount } documents` ) ;
2729 } finally {
2830 await client . close ( ) ;
2931 }
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+ enum Rating {
10+ G = "G" ,
11+ PG = "PG" ,
12+ PG_13 = "PG-13" ,
13+ R = "R" ,
14+ NR = "NOT RATED" ,
15+ }
16+
17+ interface Movie {
18+ rated : Rating ;
19+ random_review ?: string ;
20+ }
21+
22+ async function run ( ) {
23+ try {
24+ await client . connect ( ) ;
25+
26+ const database = client . db ( "sample_mflix" ) ;
27+ const movies = database . collection < Movie > ( "movies" ) ;
28+ const result = await movies . updateMany (
29+ { rated : Rating . G } ,
30+ {
31+ $set : {
32+ random_review : `After viewing I am ${
33+ 100 * Math . random ( )
34+ } % more satisfied with life.`,
35+ } ,
36+ }
37+ ) ;
38+ console . log ( `Updated ${ result . modifiedCount } documents` ) ;
39+ } finally {
40+ await client . close ( ) ;
41+ }
42+ }
43+ run ( ) . catch ( console . dir ) ;
Original file line number Diff line number Diff line change @@ -30,6 +30,24 @@ Example
3030
3131.. include:: /includes/connect-guide-note.rst
3232
33- .. literalinclude:: /code-snippets/usage-examples/updateMany.js
34- :language: javascript
35- :linenos:
33+ .. tabs::
34+
35+ .. tab:: JavaScript
36+ :tabid: javascript
37+
38+ .. literalinclude:: /code-snippets/usage-examples/updateMany.js
39+ :language: javascript
40+ :linenos:
41+
42+ .. tab:: TypeScript
43+ :tabid: typescript
44+
45+ .. literalinclude:: /code-snippets/usage-examples/updateMany.ts
46+ :language: typescript
47+ :linenos:
48+
49+ If you run the example above, you should see output like this:
50+
51+ .. code-block:: none
52+
53+ Updated 477 documents
You can’t perform that action at this time.
0 commit comments