Skip to content

DOCSP-32718: updateone UE code comments #759

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
merged 2 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
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
16 changes: 12 additions & 4 deletions source/code-snippets/usage-examples/updateOne.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Update a document

import { MongoClient } from "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,25 +12,31 @@ async function run() {
const database = client.db("sample_mflix");
const movies = database.collection("movies");

// create a filter for a movie to update
// Create a filter for movies with the title "Random Harvest"
const filter = { title: "Random Harvest" };

// this option instructs the method to create a document if no documents match the filter
/* Set the upsert option to insert a document if no documents match
the filter */
const options = { upsert: true };

// create a document that sets the plot of the movie
// Specify the update to set a value for the plot field
const updateDoc = {
$set: {
plot: `A harvest of random numbers, such as: ${Math.random()}`
},
};

// Update the first document that matches the filter
const result = await movies.updateOne(filter, updateDoc, options);

// Print the number of matching and modified documents
console.log(
`${result.matchedCount} document(s) matched the filter, updated ${result.modifiedCount} document(s)`,
);
} finally {
// Close the connection after the operation completes
await client.close();
}
}
// Run the program and print any thrown errors
run().catch(console.dir);
13 changes: 12 additions & 1 deletion source/code-snippets/usage-examples/updateOne.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// Update a document

import { MongoClient } from "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);

// Define the Movie interface
interface Movie {
plot: string;
title: string;
Expand All @@ -15,20 +18,28 @@ async function run() {
const database = client.db("sample_mflix");
const movies = database.collection<Movie>("movies");

/* Update a document that has the title "Random Harvest" to have a
plot field with the specified value */
const result = await movies.updateOne(
{ title: "Random Harvest" },
{
$set: {
plot: `A harvest of random numbers, such as: ${Math.random()}`,
},
},
/* Set the upsert option to insert a document if no documents
match the filter */
{ upsert: true }
);

// Print the number of matching and modified documents
console.log(
`${result.matchedCount} document(s) matched the filter, updated ${result.modifiedCount} document(s)`
);
} finally {
// Close the connection after the operation completes
await client.close();
}
}
// Run the program and print any thrown errors
run().catch(console.dir);