Skip to content

Docsp 32718 code whisperer bulk write #757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions source/code-snippets/usage-examples/bulkWrite.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* Bulk write operation */

// Import MongoClient from the MongoDB node driver package
const { MongoClient } = require("mongodb");

// Replace the uri string with your MongoDB deployment's connection string.
// Replace the uri string with your MongoDB deployment's connection string
const uri = "<connection string uri>";

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

// Insert a new document into the "theaters" collection
const result = await theaters.bulkWrite([
{
insertOne: {
Expand Down Expand Up @@ -40,19 +44,22 @@ async function run() {
},
},
{
// Update documents that match the specified filter
updateMany: {
filter: { "location.address.zipcode": "44011" },
update: { $set: { is_in_ohio: true } },
upsert: true,
},
},
{
// Delete a document that matches the specified filter
deleteOne: { filter: { "location.address.street1": "221b Baker St" } },
},
]);

// Log the result of the bulk write operation
console.log(result);
} finally {
// Close the database connection when the operations are completed or if an error occurs
await client.close();
}
}
Expand Down