-
Notifications
You must be signed in to change notification settings - Fork 52
DOCSP-34101-create-bulk-operations-page #948
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
gmiller-mdb
merged 15 commits into
mongodb:master
from
gmiller-mdb:DOCSP-34101-create-bulk-operations-page
Jan 7, 2025
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8833bb0
DOCSP-34101-create-bulk-operations-page
gmiller-mdb 337a25a
fleshed out page entierly
gmiller-mdb 9a804ae
fix errors add toctree
gmiller-mdb e2fe5f5
toctree
gmiller-mdb 292d517
bulk page
gmiller-mdb ddfe55f
real paths
gmiller-mdb 07fa0e2
test
gmiller-mdb e680d07
added api links
gmiller-mdb 62adb3e
fixed link and syntax
gmiller-mdb 6c6a763
syntax
gmiller-mdb 5554306
fix link
gmiller-mdb 78651fb
feedback
gmiller-mdb 47b1a0a
changed type
gmiller-mdb 0c28a03
fix indent
gmiller-mdb 4f6ad46
feedback
gmiller-mdb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
const { MongoClient, ObjectId } = require('mongodb'); | ||
|
||
const uri = '<connection string>'; // Add your MongoDB connection string here | ||
|
||
(async () => { | ||
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true }); | ||
|
||
try { | ||
await client.connect(); | ||
|
||
const database = client.db('sample_mflix'); | ||
const movies = database.collection('movies'); | ||
|
||
// Clean up collection | ||
await movies.deleteMany({}); | ||
|
||
// begin-sample-data | ||
// const movies = database.collection('movies'); | ||
|
||
const docs = [ | ||
{ title: "Inception", year: 2010, rated: "PG-13", released: "2010-07-16" }, | ||
{ title: "Interstellar", year: 2014, rated: "PG-13", released: "2014-11-07" }, | ||
{ title: "The Dark Knight", year: 2008, rated: "PG-13", released: "2008-07-18" }, | ||
{ title: "Tenet", year: 2020, rated: "PG-13", released: "2020-09-03"} | ||
]; | ||
// end-sample-data | ||
|
||
// begin-insert | ||
const bulkOps = [ | ||
{ insertOne: { document: { title: "Inception", year: 2010, rated: "PG-13", released: "2010-07-16" } } }, | ||
{ insertOne: { document: { title: "Interstellar", year: 2014, rated: "PG-13", released: "2014-11-07" } } }, | ||
{ insertOne: { document: { title: "The Dark Knight", year: 2008, rated: "PG-13", released: "2008-07-18" } } }, | ||
{ insertOne: { document: { title: "Tenet", year: 2020, rated: "PG-13", released: "2020-09-03" } } } | ||
]; | ||
|
||
await movies.bulkWrite(bulkOps); | ||
// end-insert | ||
|
||
await movies.insertMany(docs); | ||
|
||
// Inserting additional movies | ||
const additionalMovies = [ | ||
{ title: "Dunkirk", year: 2017, rated: "PG-13", released: "2017-07-21" }, | ||
{ title: "Memento", year: 2000, rated: "R", released: "2000-09-05" } | ||
]; | ||
await movies.insertMany(additionalMovies); | ||
|
||
|
||
// begin-replace | ||
const replaceOperations = [ | ||
{ | ||
replaceOne: { | ||
filter: { title: "The Dark Knight" }, | ||
replacement: { title: "The Dark Knight Rises", year: 2012, rating: "PG-13" }, | ||
upsert: false | ||
} | ||
}, | ||
{ | ||
replaceOne: { | ||
filter: { title: "Inception" }, | ||
replacement: { title: "Inception Reloaded", year: 2010, rating: "PG-13" }, | ||
upsert: false | ||
} | ||
} | ||
]; | ||
|
||
const replace_result = await movies.bulkWrite(replaceOperations); | ||
// end-replace | ||
|
||
|
||
// begin-update | ||
const updateOperations = [ | ||
{ | ||
updateOne: { | ||
filter: { title: "Interstellar" }, | ||
update: { $set: { title: "Interstellar Updated", genre: "Sci-Fi Adventure" } }, | ||
upsert: true | ||
} | ||
}, | ||
{ | ||
updateMany: { | ||
filter: { rated: "PG-13" }, | ||
update: { $set: { rated: "PG-13 Updated", genre: "Updated Genre" } } | ||
} | ||
} | ||
]; | ||
|
||
const update_result = await movies.bulkWrite(updateOperations); | ||
|
||
console.log(`Matched documents: ${result3.matchedCount}`); | ||
console.log(`Modified documents: ${result3.modifiedCount}`); | ||
// end-update | ||
|
||
|
||
// begin-delete | ||
const deleteOperations = [ | ||
{ | ||
deleteOne: { | ||
filter: { title: "Dunkirk" } | ||
} | ||
}, | ||
{ | ||
deleteMany: { | ||
filter: { rated: "R" } | ||
} | ||
} | ||
]; | ||
|
||
|
||
const delete_result = await movies.bulkWrite(deleteOperations); | ||
|
||
console.log(`Deleted documents: ${result4.deletedCount}`); | ||
// end-delete | ||
|
||
|
||
console.log("Operations completed successfully."); | ||
} finally { | ||
await client.close(); | ||
} | ||
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.