File tree Expand file tree Collapse file tree 3 files changed +56
-4
lines changed
code-snippets/usage-examples Expand file tree Collapse file tree 3 files changed +56
-4
lines changed Original file line number Diff line number Diff line change @@ -12,8 +12,8 @@ async function run() {
1212
1313 const database = client . db ( "sample_mflix" ) ;
1414 const movies = database . collection ( "movies" ) ;
15- // Query for all movies with the title "Santa Claus "
16- const query = { title : "Santa Claus" } ;
15+ // Query for all movies with a title containing the string "Santa"
16+ const query = { title : { $regex : "Santa" } } ;
1717
1818 const result = await movies . deleteMany ( query ) ;
1919 console . log ( "Deleted " + result . deletedCount + " documents" ) ;
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 Movie {
10+ title : string ;
11+ }
12+
13+ async function run ( ) {
14+ try {
15+ await client . connect ( ) ;
16+
17+ const database = client . db ( "sample_mflix" ) ;
18+ const movies = database . collection < Movie > ( "movies" ) ;
19+ const result = await movies . deleteMany ( { title : { $regex : "Santa" } } ) ;
20+ console . log ( "Deleted " + result . deletedCount + " documents" ) ;
21+ } finally {
22+ await client . close ( ) ;
23+ }
24+ }
25+ run ( ) . catch ( console . dir ) ;
Original file line number Diff line number Diff line change @@ -37,5 +37,32 @@ match and delete movies with the title "Santa Claus".
3737
3838.. include:: /includes/connect-guide-note.rst
3939
40- .. literalinclude:: /code-snippets/usage-examples/deleteMany.js
41- :language: javascript
40+ .. tabs::
41+
42+ .. tab:: JavaScript
43+ :tabid: javascript
44+
45+ .. literalinclude:: /code-snippets/usage-examples/deleteMany.js
46+ :language: javascript
47+ :linenos:
48+
49+ .. tab:: TypeScript
50+ :tabid: typescript
51+
52+ .. literalinclude:: /code-snippets/usage-examples/deleteMany.ts
53+ :language: typescript
54+ :linenos:
55+
56+ The first time you run the preceding example, you should see output that
57+ resembles the following:
58+
59+ .. code-block:: none
60+
61+ Deleted 19 documents
62+
63+ On subsequent runs of the example, as you already deleted all relevant
64+ documents, you should see output that resembles the following:
65+
66+ .. code-block:: none
67+
68+ Deleted 0 documents
You can’t perform that action at this time.
0 commit comments