Skip to content

Commit 6d1c8ac

Browse files
committed
DOCSP-32718: insertOne comments
1 parent f09e867 commit 6d1c8ac

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,30 @@ import { MongoClient } from "mongodb";
33
// Replace the uri string with your MongoDB deployment's connection string.
44
const uri = "<connection string uri>";
55

6+
// Create a new instance of the MongoClient using the provided URI
67
const client = new MongoClient(uri);
78

9+
// Define a function to interact with the MongoDB database
810
async function run() {
911
try {
12+
// Connect to the "insertDB" database and access its "haiku" collection
1013
const database = client.db("insertDB");
1114
const haiku = database.collection("haiku");
12-
// create a document to insert
15+
16+
// Create a document to insert
1317
const doc = {
1418
title: "Record of a Shriveled Datum",
1519
content: "No bytes, no problem. Just insert a document, in MongoDB",
1620
}
21+
// Insert the defined document into the "haiku" collection
1722
const result = await haiku.insertOne(doc);
1823

24+
// Print the ID of the inserted document
1925
console.log(`A document was inserted with the _id: ${result.insertedId}`);
2026
} finally {
27+
// Close the MongoDB client connection
2128
await client.close();
2229
}
2330
}
31+
// Call the "run" function and handle errors with console.dir
2432
run().catch(console.dir);

0 commit comments

Comments
 (0)