Skip to content

Commit 41c4ff7

Browse files
caitlindaveynorareidy
authored andcommitted
Docsp 32718 code whisperer bulk write (mongodb#757)
* Bulk Write Snippet Update * Bulk Write Snippet Update * Update source/code-snippets/usage-examples/bulkWrite.js Co-authored-by: Nora Reidy <[email protected]> --------- Co-authored-by: Nora Reidy <[email protected]>
1 parent 5cc576f commit 41c4ff7

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
/* Bulk write operation */
2+
3+
// Import MongoClient from the MongoDB node driver package
14
const { MongoClient } = require("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
47
const uri = "<connection string uri>";
58

69
const client = new MongoClient(uri);
@@ -10,6 +13,7 @@ async function run() {
1013
const database = client.db("sample_mflix");
1114
const theaters = database.collection("theaters");
1215

16+
// Insert a new document into the "theaters" collection
1317
const result = await theaters.bulkWrite([
1418
{
1519
insertOne: {
@@ -40,19 +44,22 @@ async function run() {
4044
},
4145
},
4246
{
47+
// Update documents that match the specified filter
4348
updateMany: {
4449
filter: { "location.address.zipcode": "44011" },
4550
update: { $set: { is_in_ohio: true } },
4651
upsert: true,
4752
},
4853
},
4954
{
55+
// Delete a document that matches the specified filter
5056
deleteOne: { filter: { "location.address.street1": "221b Baker St" } },
5157
},
5258
]);
53-
59+
// Log the result of the bulk write operation
5460
console.log(result);
5561
} finally {
62+
// Close the database connection when the operations are completed or if an error occurs
5663
await client.close();
5764
}
5865
}

0 commit comments

Comments
 (0)