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.
14import { MongoClient } from "mongodb" ;
25
36// Replace the uri string with your MongoDB deployment's connection string.
47const uri = "<connection string uri>" ;
58
9+ // Create a new client and connect to MongoDB.
610const client = new MongoClient ( uri ) ;
711
812async function run ( ) {
913 try {
1014 const database = client . db ( "sample_mflix" ) ;
1115 const movies = database . collection ( "movies" ) ;
12- // Query for all movies with a title containing the string "Santa"
13- const query = { title : { $regex : "Santa" } } ;
1416
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" } } ;
1520 const result = await movies . deleteMany ( query ) ;
21+
22+ // Print the number of deleted documents
1623 console . log ( "Deleted " + result . deletedCount + " documents" ) ;
1724 } finally {
25+ // Close the client after the operation completes
1826 await client . close ( ) ;
1927 }
2028}
29+ // Run the program and print any thrown exceptions
2130run ( ) . 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
14import { MongoClient } from "mongodb" ;
25
3- // Replace the uri string with your MongoDB deployment's connection string.
6+ // Replace the uri string with your MongoDB deployment's connection string
47const uri = "<connection string uri>" ;
58
9+ // Create a new client and connect to MongoDB
610const client = new MongoClient ( uri ) ;
711
8- interface Movie {
9- title : string ;
10- }
11-
1212async function run ( ) {
1313 try {
1414 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 */
1619 const result = await movies . deleteMany ( { title : { $regex : "Santa" } } ) ;
20+
21+ // Print the number of deleted documents
1722 console . log ( "Deleted " + result . deletedCount + " documents" ) ;
1823 } finally {
24+ // Close the client after the operation completes
1925 await client . close ( ) ;
2026 }
2127}
28+ // Run the program and print any thrown exceptions
2229run ( ) . catch ( console . dir ) ;
You can’t perform that action at this time.
0 commit comments