Skip to content

DOCSP-32718: insertOne comments #758

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 5, 2023
Merged
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion source/code-snippets/usage-examples/insertOne.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,30 @@ import { MongoClient } from "mongodb";
// Replace the uri string with your MongoDB deployment's connection string.
const uri = "<connection string uri>";

// Create a new instance of the MongoClient using the provided URI
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Create a new instance of the MongoClient using the provided URI
// Create a new client and connect to MongoDB

const client = new MongoClient(uri);

// Define a function to interact with the MongoDB database
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S; remove

async function run() {
try {
// Connect to the "insertDB" database and access its "haiku" collection
const database = client.db("insertDB");
const haiku = database.collection("haiku");
// create a document to insert

// Create a document to insert
const doc = {
title: "Record of a Shriveled Datum",
content: "No bytes, no problem. Just insert a document, in MongoDB",
}
// Insert the defined document into the "haiku" collection
const result = await haiku.insertOne(doc);

// Print the ID of the inserted document
console.log(`A document was inserted with the _id: ${result.insertedId}`);
} finally {
// Close the MongoDB client connection
await client.close();
}
}
// Call the "run" function and handle errors with console.dir
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Call the "run" function and handle errors with console.dir
// Run the function and handle any errors

run().catch(console.dir);