Skip to content

Commit 9df7f89

Browse files
(DOCSP 17936) Delete Many TS Example (#238)
Co-authored-by: Chris Bush <chris.bush@10gen.com>
1 parent 9b22735 commit 9df7f89

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff 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");
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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);

source/usage-examples/deleteMany.txt

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)