File tree Expand file tree Collapse file tree 1 file changed +8
-1
lines changed
source/code-snippets/usage-examples Expand file tree Collapse file tree 1 file changed +8
-1
lines changed Original file line number Diff line number Diff line change @@ -3,22 +3,29 @@ import { MongoClient } from "mongodb";
33// Replace the uri string with your MongoDB deployment's connection string.
44const uri = "<connection string uri>" ;
55
6+ // Create a new client and connect to MongoDB
67const client = new MongoClient ( uri ) ;
78
89async function run ( ) {
910 try {
11+ // Connect to the "insertDB" database and access its "haiku" collection
1012 const database = client . db ( "insertDB" ) ;
1113 const haiku = database . collection ( "haiku" ) ;
12- // create a document to insert
14+
15+ // Create a document to insert
1316 const doc = {
1417 title : "Record of a Shriveled Datum" ,
1518 content : "No bytes, no problem. Just insert a document, in MongoDB" ,
1619 }
20+ // Insert the defined document into the "haiku" collection
1721 const result = await haiku . insertOne ( doc ) ;
1822
23+ // Print the ID of the inserted document
1924 console . log ( `A document was inserted with the _id: ${ result . insertedId } ` ) ;
2025 } finally {
26+ // Close the MongoDB client connection
2127 await client . close ( ) ;
2228 }
2329}
30+ // Run the function and handle any errors
2431run ( ) . catch ( console . dir ) ;
You can’t perform that action at this time.
0 commit comments