Skip to content

Commit 89f96cf

Browse files
authored
DOCSP-32718: updateone UE code comments (#759)
* DOCSP-32718: updateone UE code comments * fixes
1 parent 5b0652a commit 89f96cf

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed
Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
// Update a document
2+
13
import { MongoClient } from "mongodb";
24

3-
// Replace the uri string with your MongoDB deployment's connection string.
5+
// Replace the uri string with your MongoDB deployment's connection string
46
const uri = "<connection string uri>";
57

68
const client = new MongoClient(uri);
@@ -10,25 +12,31 @@ async function run() {
1012
const database = client.db("sample_mflix");
1113
const movies = database.collection("movies");
1214

13-
// create a filter for a movie to update
15+
// Create a filter for movies with the title "Random Harvest"
1416
const filter = { title: "Random Harvest" };
1517

16-
// this option instructs the method to create a document if no documents match the filter
18+
/* Set the upsert option to insert a document if no documents match
19+
the filter */
1720
const options = { upsert: true };
1821

19-
// create a document that sets the plot of the movie
22+
// Specify the update to set a value for the plot field
2023
const updateDoc = {
2124
$set: {
2225
plot: `A harvest of random numbers, such as: ${Math.random()}`
2326
},
2427
};
2528

29+
// Update the first document that matches the filter
2630
const result = await movies.updateOne(filter, updateDoc, options);
31+
32+
// Print the number of matching and modified documents
2733
console.log(
2834
`${result.matchedCount} document(s) matched the filter, updated ${result.modifiedCount} document(s)`,
2935
);
3036
} finally {
37+
// Close the connection after the operation completes
3138
await client.close();
3239
}
3340
}
41+
// Run the program and print any thrown errors
3442
run().catch(console.dir);

source/code-snippets/usage-examples/updateOne.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
// Update a document
2+
13
import { MongoClient } from "mongodb";
24

3-
// Replace the uri string with your MongoDB deployment's connection string.
5+
// Replace the uri string with your MongoDB deployment's connection string
46
const uri = "<connection string uri>";
57

68
const client = new MongoClient(uri);
79

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

21+
/* Update a document that has the title "Random Harvest" to have a
22+
plot field with the specified value */
1823
const result = await movies.updateOne(
1924
{ title: "Random Harvest" },
2025
{
2126
$set: {
2227
plot: `A harvest of random numbers, such as: ${Math.random()}`,
2328
},
2429
},
30+
/* Set the upsert option to insert a document if no documents
31+
match the filter */
2532
{ upsert: true }
2633
);
34+
35+
// Print the number of matching and modified documents
2736
console.log(
2837
`${result.matchedCount} document(s) matched the filter, updated ${result.modifiedCount} document(s)`
2938
);
3039
} finally {
40+
// Close the connection after the operation completes
3141
await client.close();
3242
}
3343
}
44+
// Run the program and print any thrown errors
3445
run().catch(console.dir);

0 commit comments

Comments
 (0)