File tree Expand file tree Collapse file tree 2 files changed +24
-8
lines changed
source/code-snippets/usage-examples Expand file tree Collapse file tree 2 files changed +24
-8
lines changed Original file line number Diff line number Diff line change
1
+ /* Delete multiple documents */
2
+
3
+ // Import the MongoClient type from the mongodb package.
1
4
import { MongoClient } from "mongodb" ;
2
5
3
6
// Replace the uri string with your MongoDB deployment's connection string.
4
7
const uri = "<connection string uri>" ;
5
8
9
+ // Create a new client and connect to MongoDB.
6
10
const client = new MongoClient ( uri ) ;
7
11
8
12
async function run ( ) {
9
13
try {
10
14
const database = client . db ( "sample_mflix" ) ;
11
15
const movies = database . collection ( "movies" ) ;
12
- // Query for all movies with a title containing the string "Santa"
13
- const query = { title : { $regex : "Santa" } } ;
14
16
17
+ /* Delete all documents that match the specified regular
18
+ expression in the title field from the "movies" collection */
19
+ const query = { title : { $regex : "Santa" } } ;
15
20
const result = await movies . deleteMany ( query ) ;
21
+
22
+ // Print the number of deleted documents
16
23
console . log ( "Deleted " + result . deletedCount + " documents" ) ;
17
24
} finally {
25
+ // Close the client after the operation completes
18
26
await client . close ( ) ;
19
27
}
20
28
}
29
+ // Run the program and print any thrown exceptions
21
30
run ( ) . catch ( console . dir ) ;
Original file line number Diff line number Diff line change
1
+ /* Delete multiple documents */
2
+
3
+ // Import the MongoClient type from the mongodb package
1
4
import { MongoClient } from "mongodb" ;
2
5
3
- // Replace the uri string with your MongoDB deployment's connection string.
6
+ // Replace the uri string with your MongoDB deployment's connection string
4
7
const uri = "<connection string uri>" ;
5
8
9
+ // Create a new client and connect to MongoDB
6
10
const client = new MongoClient ( uri ) ;
7
11
8
- interface Movie {
9
- title : string ;
10
- }
11
-
12
12
async function run ( ) {
13
13
try {
14
14
const database = client . db ( "sample_mflix" ) ;
15
- const movies = database . collection < Movie > ( "movies" ) ;
15
+ const movies = database . collection ( "movies" ) ;
16
+
17
+ /* Delete all documents that match the specified regular
18
+ expression in the title field from the "movies" collection */
16
19
const result = await movies . deleteMany ( { title : { $regex : "Santa" } } ) ;
20
+
21
+ // Print the number of deleted documents
17
22
console . log ( "Deleted " + result . deletedCount + " documents" ) ;
18
23
} finally {
24
+ // Close the client after the operation completes
19
25
await client . close ( ) ;
20
26
}
21
27
}
28
+ // Run the program and print any thrown exceptions
22
29
run ( ) . catch ( console . dir ) ;
You can’t perform that action at this time.
0 commit comments