File tree Expand file tree Collapse file tree 1 file changed +9
-2
lines changed
source/code-snippets/usage-examples Expand file tree Collapse file tree 1 file changed +9
-2
lines changed Original file line number Diff line number Diff line change
1
+ /* Bulk write operation */
2
+
3
+ // Import MongoClient from the MongoDB node driver package
1
4
const { MongoClient } = require ( "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
6
9
const client = new MongoClient ( uri ) ;
@@ -10,6 +13,7 @@ async function run() {
10
13
const database = client . db ( "sample_mflix" ) ;
11
14
const theaters = database . collection ( "theaters" ) ;
12
15
16
+ // Insert a new document into the "theaters" collection
13
17
const result = await theaters . bulkWrite ( [
14
18
{
15
19
insertOne : {
@@ -40,19 +44,22 @@ async function run() {
40
44
} ,
41
45
} ,
42
46
{
47
+ // Update documents that match the specified filter
43
48
updateMany : {
44
49
filter : { "location.address.zipcode" : "44011" } ,
45
50
update : { $set : { is_in_ohio : true } } ,
46
51
upsert : true ,
47
52
} ,
48
53
} ,
49
54
{
55
+ // Delete a document that matches the specified filter
50
56
deleteOne : { filter : { "location.address.street1" : "221b Baker St" } } ,
51
57
} ,
52
58
] ) ;
53
-
59
+ // Log the result of the bulk write operation
54
60
console . log ( result ) ;
55
61
} finally {
62
+ // Close the database connection when the operations are completed or if an error occurs
56
63
await client . close ( ) ;
57
64
}
58
65
}
You can’t perform that action at this time.
0 commit comments