File tree Expand file tree Collapse file tree 1 file changed +9
-1
lines changed
source/code-snippets/usage-examples Expand file tree Collapse file tree 1 file changed +9
-1
lines changed Original file line number Diff line number Diff line change @@ -3,22 +3,30 @@ import { MongoClient } from "mongodb";
3
3
// Replace the uri string with your MongoDB deployment's connection string.
4
4
const uri = "<connection string uri>" ;
5
5
6
+ // Create a new instance of the MongoClient using the provided URI
6
7
const client = new MongoClient ( uri ) ;
7
8
9
+ // Define a function to interact with the MongoDB database
8
10
async function run ( ) {
9
11
try {
12
+ // Connect to the "insertDB" database and access its "haiku" collection
10
13
const database = client . db ( "insertDB" ) ;
11
14
const haiku = database . collection ( "haiku" ) ;
12
- // create a document to insert
15
+
16
+ // Create a document to insert
13
17
const doc = {
14
18
title : "Record of a Shriveled Datum" ,
15
19
content : "No bytes, no problem. Just insert a document, in MongoDB" ,
16
20
}
21
+ // Insert the defined document into the "haiku" collection
17
22
const result = await haiku . insertOne ( doc ) ;
18
23
24
+ // Print the ID of the inserted document
19
25
console . log ( `A document was inserted with the _id: ${ result . insertedId } ` ) ;
20
26
} finally {
27
+ // Close the MongoDB client connection
21
28
await client . close ( ) ;
22
29
}
23
30
}
31
+ // Call the "run" function and handle errors with console.dir
24
32
run ( ) . catch ( console . dir ) ;
You can’t perform that action at this time.
0 commit comments