File tree Expand file tree Collapse file tree 1 file changed +10
-3
lines changed
source/code-snippets/usage-examples Expand file tree Collapse file tree 1 file changed +10
-3
lines changed Original file line number Diff line number Diff line change
1
+ // Delete a document
2
+
1
3
import { MongoClient } from "mongodb" ;
2
4
3
- // Replace the uri string with your MongoDB deployment's connection string.
5
+ // Replace the uri string with your MongoDB deployment's connection string
4
6
const uri = "<connection string uri>" ;
5
7
6
8
const client = new MongoClient ( uri ) ;
@@ -10,17 +12,22 @@ async function run() {
10
12
const database = client . db ( "sample_mflix" ) ;
11
13
const movies = database . collection ( "movies" ) ;
12
14
13
- // Query for a movie that has title "Annie Hall"
15
+ /* Delete the first document in the "movies" collection that matches
16
+ the specified query document */
14
17
const query = { title : "Annie Hall" } ;
15
-
16
18
const result = await movies . deleteOne ( query ) ;
19
+
20
+ /* Print a message that indicates whether the operation deleted a
21
+ document */
17
22
if ( result . deletedCount === 1 ) {
18
23
console . log ( "Successfully deleted one document." ) ;
19
24
} else {
20
25
console . log ( "No documents matched the query. Deleted 0 documents." ) ;
21
26
}
22
27
} finally {
28
+ // Close the connection after the operation completes
23
29
await client . close ( ) ;
24
30
}
25
31
}
32
+ // Run the program and print any thrown exceptions
26
33
run ( ) . catch ( console . dir ) ;
You can’t perform that action at this time.
0 commit comments